Click or drag to resize

Searching for a Location

Verizon Connect Logo
Print this page
Learn more about Verizon Connect GeoBase.
Get information about the latest release
Overview

After completing the steps outlined in this topic our application will be able to use forward geocoding to determine a street address, and will mark that address with an BalloonPushPin.

Address Lookup and Map Re-Centering

This function will take an address string from textboxSearch, pass it to the GeoCoder to lookup the address, and then center and zoom the main map on the first resulting match.

In the Designer view, add a Click event to buttonSearch. Switch to the Code view and add the following code to the new event.

C#
private void buttonSearch_Click(object sender, EventArgs e) {
  // change this next line if you're not in the USA - you could also
  // consider placing a combo-box on the form and allowing the user
  // to select a country...
  GeocodeAddress[] addresses = GeoCoder.GeoCode(textBoxSearch.Text, Country.USA);

  // it's possible that the GeoCoder didn't return any addresses. We can
  // only center and zoom the map if we got (at least) one result.
  if (addresses.Length >= 1) {
    mapMain.Center = addresses[0].Location;
    mapMain.Zoom = ZoomLevel.StreetLevel;

    // force an update on the map center & map zoom level
    mapMain.Invalidate();
  }
}
Caution note Caution

Because GeoCode(String, Country) requires two parameters: an address (string) and a country (of type Country). The country has been hard-coded into the above method call.

If you are searching for an address outside the United States you'll need to change Country.USA to the country you are using. See the Country enumeration for more information.

Displaying Information About an Address

A BalloonPushPin object can be used to mark an address on the map with relevant textual information.

The following code creates a new BalloonPushPin object (with the default Icon positioned at the address. By default a BalloonPushPin will also display the address of the location.

Return to the Code View, and modify the Click event for buttonSearch to match the following code snippet:

C#
private void buttonSearch_Click(object sender, EventArgs e) {
  // GeoCode the address in the search box...
  GeocodeAddress[] addresses = GeoCoder.GeoCode(textBoxSearch.Text, Country.USA);

  // zoom and center on the top GeoCoder result (if we have one)
  if (addresses.Length >= 1) {
      mapMain.Center = addresses[0].Location;
      mapMain.Zoom = Telogis.GeoBase.ZoomLevel.StreetLevel;

      // remove the old searchMarker from the renderList. This ensures
      // that only the most recent marker is drawn on the map
      if (searchMarker != null) {
          renderList.Remove(searchMarker);
      }
      searchMarker = new Telogis.GeoBase.BalloonPushPin(mapMain.Center);
      searchMarker.Icon = Telogis.GeoBase.ImageUtils.Icons.PinVerticalRed;
      renderList.Add(searchMarker);
      mapMain.Invalidate();
   }
 }
Next Topic

In the next topic, Creating and Optimizing a Route, we will add simple routing capabilities to our application.