(Not Signed In)

Sign In

Resources

Forward geocoding (performing an address search) using SilverStream

The process of searching for an address on a map is known as a 'forward geocode'. This tutorial will walk you through the creation of a simple Silverlight application that allows the user to search for an address on a map. This tutorial is one of many Telogis SilverStream tutorials.

This guide is broken into the following steps:

  1. Create a Visual Studio project
  2. Configure a GeoStream server using C# code
  3. Add a map, textbox, search button and listbox using XAML code
  4. Perform a forward geocode (address search) using C# code

Create a Silverlight project and configure a GeoStream server

Make sure that the file clientaccesspolicy.xml exists in your Inetpub\wwwroot directory.  Create a new Silverlight project named 'GeocodeTutorial' and add a reference to Telogis.GeoBase.SilverStream.dll. Configure a GeoStream server.

See the Create a SilverStream-Specific Visual Studio Project tutorial for an illustrated walk-through.

Add user controls

Open MainPage.xaml and add the following attributes to the UserControl tag. These attributes allow your XAML code to access the local namespace (GeocodeTutorial) and the Telogis.GeoBase namespace.

xmlns:Local="clr-namespace:GeocodeTutorial"
xmlns:GeoBase="clr-namespace:Telogis.GeoBase;assembly=Telogis.GeoBase.Silverlight"

Modify the code between the Grid tags to match the code snippet below. This code creates a 3-row layout containing a map, search box and search button, and a listbox for search results.

The x:Name attributes allow the XAML components to be accessed from C# code. The KeyDown, Click and SelectionChanged attributes point to C# methods that will be called when the corresponding OnKeyDown, OnClick and OnSelectionChanged events are fired for the respective controls.

<Grid x:Name="LayoutRoot">

  <Grid.RowDefinitions>
    <RowDefinition Height="400" />
    <RowDefinition Height="25" />
    <RowDefinition Height="200" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>

  <Border Grid.Row="0">
    <GeoBase:Map x:Name="MainMap" Center="36,-122" Zoom="4">
    </GeoBase:Map>
  </Border>

  <StackPanel Grid.Row="1" Orientation="Horizontal">
    <TextBlock VerticalAlignment="Center">Enter a US address to search for:</TextBlock>
    <TextBox x:Name="tbSrchAddress" Width="300" KeyDown="tbSrchAddress_KeyDown"></TextBox>
    <Button Content="Search" Click="Button_Click"></Button>
  </StackPanel>

  <ListBox Grid.Row="2"
    x:Name="AddressResults"
    SelectionChanged="AddressResults_SelectionChanged"
    ScrollViewer.VerticalScrollBarVisibility="Auto">
  </ListBox>

</Grid>

Forward geocoding

Open MainPage.xaml.cs and add the following two namespace directives to the top of the source file.

using Telogis.GeoBase;
using Telogis.GeoBase.GeoStream;

Inside the MainPage class add the following method. This method will (in conjunction with an inline delegate method) perform a forward geocode and place the geocode results in the listbox.

private void Forward_Geocode() 
{
  GeoCoder.GeoCode(tbSrchAddress.Text, Country.USA, 
    delegate(object o, JSONRequestEventArgs ee) 
    {
      Dispatcher.BeginInvoke(delegate() 
      {
        AddressResults.Items.Clear();
        foreach (GeocodeAddress a in (List<object>)o)
          AddressResults.Items.Add(a);
      });
    });
}

Add the following methods to the MainPage class. These methods are event-driven, the particular events were defined earlier in XAML code.

  // If the user hits Enter and we have a search string, perform a forward geocode
  private void tbSrchAddress_KeyDown(object sender, KeyEventArgs e) {
    if (e.Key == Key.Enter && !string.IsNullOrEmpty(tbSrchAddress.Text))
      Forward_Geocode();
  }

  // If the user clicks Search and we have a search string, perform a forward geocode
  private void Button_Click(object sender, RoutedEventArgs e) {
    if (!string.IsNullOrEmpty(tbSrchAddress.Text))
      Forward_Geocode();
  }

  // If the user clicks a search result, center and zoom to the location
  private void AddressResults_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    if (AddressResults.SelectedItem != null) {
        GeocodeAddress ga = ((GeocodeAddress)AddressResults.SelectedItem);

        // how far we zoom the map depends on whether the search result 
        // describes a city or not
        if (ga.MatchType == GeoCodeMatch.MATCH_CITY) MainMap.Zoom = 10;
        else MainMap.Zoom = 15;

        MainMap.Center = ga.Location;
    }
}

Testing

Press F5 to build and run your application. Enter a partial, or full, address and hit Enter or click the Search button. A list of results will be displayed in the list box. Select a result from the list box, and the map will zoom and center on the address.

Next step

The next tutorial, Creating and displaying markers on a SilverStream map, demonstrates how to display the location of various points of interest (such as tourist sites or vehicles) by drawing markers and placing images at specific map locations. Alternatively you can skip ahead to another Telogis SilverStream tutorial.

Published, Jun 28th, 15:40

Tagged under: silverlight forward geocoding