Geocode
- 4 minutes to read
The Map control supports Microsoft’s Bing Geocode and the OpenStreetMap Geocode services, which provide information associated with the geographic point on a map (address, postcode, etc.) based on a point’s location (latitude and longitude coordinates).
The Bing Geocode and OpenStreetMap Geocode data providers provide the geocoding functionality and are represented by the BingGeocodeDataProvider and OsmGeocodeDataProvider classes. The sections below explain how to use the BingGeocodeDataProvider in the map control.
Important
Due to Bing canceling the SOAP service on July 30, 2017, the Map Control’s Bing Geocode provider does not work correctly in version 16.1 and earlier.
Enabling Geocode
Do the following to enable geocoding in the Map control:
Create an information layer and add it to the map.
The information layer provides vector elements that represent GIS data obtained from the Search service in the Map control. See Layers for more information.
- Create a BingGeocodeDataProvider or OsmGeocodeDataProvider instance and assign it to the InformationLayer.DataProvider property.
Specify the Bing Maps key for the Bing provider using the BingMapDataProviderBase.BingKey property.
Note
Go to The Bing Maps Portal website to create a developer account.
Refer to How to: Get a Bing Maps Key to learn more about how to register a Bing Maps account and create a key for it.
The code snippet below shows how to do this.
<dxm:InformationLayer.DataProvider>
<dxm:BingGeocodeDataProvider BingKey="{Binding Source={StaticResource BingKey}}"
MaxVisibleResultCount="1"/>
</dxm:InformationLayer.DataProvider>
When the Map control is connected to the Bing Geocode service, you can use the pushpin’s tooltip to obtain information about a geographic point:
Click the desired location on a map.
This generates a pushpin at the mouse position (the InformationDataProviderBase.ProcessMouseEvents and InformationDataProviderBase.GenerateLayerItems properties are set to true by default).
- Hover the mouse cursor over the pushpin to see its tooltip.
The image below shows how this works for “Kennedyville”.
You can also use the InformationDataProviderBase.MaxVisibleResultCount property to specify how many tooltips can be shown on a map simultaneously.
Using a Custom UI
The map control allows you to obtain additional results (address, name, entity type, etc.) from the Bing Geocode service for a specified location.
Note
Set the InformationDataProviderBase.ProcessMouseEvents property to false if you do not want to generate pushpins when you click on the map.
To start geocoding for a location, call the BingGeocodeDataProvider.RequestLocationInformation or OsmGeocodeDataProvider.RequestLocationInformation method.
For instance, you have a UI that consists of 2 text boxes named “tbLatitude” and “tbLongitude”, and a button named “requestLocation”. To start a search, click the Request Location button. This calls the RequestLocationInformation method.
The results are stored by the RequestResultBase object’s GeocodeRequestResult descendant within the LocationInformationReceived event handler arguments’ LocationInformationReceivedEventArgs.Result.
The results contain the entity type, address, and coordinates associated with the geographic location.
GeoPoint location = new GeoPoint(40, -120);
public GeoPoint Location {
get { return location; }
set { location = value;}
}
private void requestLocation_Click(object sender, RoutedEventArgs e) {
geocodeProvider.RequestLocationInformation(Location, null);
}
Processing Geocode Results
To get the geocode results for a specified location, handle the BingGeocodeDataProvider.LocationInformationReceived or OsmGeocodeDataProvider.LocationInformationReceived event. The results are stored by the RequestResultBase object’s GeocodeRequestResult descendant within the LocationInformationReceived event handler’s LocationInformationReceivedEventArgs.
The following code snippet shows how to handle the event.
<dxm:BingGeocodeDataProvider x:Name="geocodeProvider" ProcessMouseEvents="False"
BingKey="{Binding Source={StaticResource YourBingKey}}"
LocationInformationReceived="geocodeProvider_LocationInformationReceived"/>
The results for Latitude - “41.27” and Longitude - “-96.05” are shown in the image below.