Back to Devexpress

Geocode

windowsforms-16712-controls-and-libraries-map-control-gis-data-geocode.md

latest10.9 KB
Original Source

Geocode

  • Jul 08, 2025
  • 5 minutes to read

The Map control supports Microsoft’s Azure 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 Azure Geocode and OpenStreetMap Geocode data providers provide the geocoding functionality and are represented by the AzureGeocodeDataProvider and OsmGeocodeDataProvider classes. The sections below explain how to use the AzureGeocodeDataProvider in the map control.

Enabling Geocode

Do the following to enable geocoding in the Map control:

The code snippet below shows how to do this.

View Example

csharp
// Create a geocode data provider.
geocodeProvider = new AzureGeocodeDataProvider {
    AzureKey = yourKey,
    MaxVisibleResultCount = 1
};
geocodeProvider.LocationInformationReceived += OnLocationInformationReceived;
geocodeProvider.LayerItemsGenerating += OnLayerItemsGenerating;
informationLayer.DataProvider = geocodeProvider;
vb
' Create a geocode data provider.
geocodeProvider = New AzureGeocodeDataProvider With {.AzureKey = yourKey, .MaxVisibleResultCount = 1}
AddHandler geocodeProvider.LocationInformationReceived, AddressOf OnLocationInformationReceived
AddHandler geocodeProvider.LayerItemsGenerating, AddressOf OnLayerItemsGenerating
informationLayer.DataProvider = geocodeProvider

When the Map control is connected to the Azure Geocode services, you can use the pushpin’s tooltip to obtain information about a geographic point:

  • Click the desired location on a map.

  • Hover the mouse cursor over the pushpin to see its tooltip.

The image below shows a tooltip for the pushpin at “Mecklenburgh Square”.

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 Azure Geocode services 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 AzureGeocodeDataProvider.RequestLocationInformation or OsmGeocodeDataProvider.RequestLocationInformation method overloads.

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’s LocationInformationReceivedEventArgs.

The results contain the entity type, address, and coordinates associated with the geographic location.

View Example

csharp
private void requestLocation_Click(object sender, System.EventArgs e) {
    GeoPoint searchPoint;
    if(TryGetLocationArguments(out searchPoint)) {
        geocodeProvider.RequestLocationInformation(searchPoint, 0);
    }
}

bool TryGetLocationArguments(out GeoPoint point) {
    double latitude;
    double longitude;
    if (
        TryConvertLocationCoordinate(teLatitude.Text, minLatitude, maxLatitude, latitudeName, out latitude) && 
        TryConvertLocationCoordinate(teLongitude.Text, minLongitude, maxLongitude, longitudeName, out longitude)) {
        point = new GeoPoint(latitude, longitude);
        return true;
    }
    point = null;
    return false;
}

bool TryConvertLocationCoordinate(string str, double minValue, double maxValue, string valueName, out double value) {
    double convertedValue = String.IsNullOrEmpty(str)
        ? 0
        : Double.Parse(str);
    if((convertedValue > maxValue) || (convertedValue < minValue)) {
        MessageBox.Show(String.Format(msgMinMaxErrorFormatString, valueName, minValue, maxValue));
        value = 0;
        return false;
    }
    value = convertedValue;
    return true;

}
vb
Private Sub requestLocation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGeocode.Click
    Dim searchPoint As GeoPoint = Nothing
    If TryGetLocationArguments(searchPoint) Then
        geocodeProvider.RequestLocationInformation(searchPoint, 0)
    End If
End Sub

Private Function TryGetLocationArguments(ByRef point As GeoPoint) As Boolean
    Dim latitude As Double = Nothing
    Dim longitude As Double = Nothing
    If TryConvertLocationCoordinate(teLatitude.Text, minLatitude, maxLatitude, latitudeName, latitude) AndAlso TryConvertLocationCoordinate(teLongitude.Text, minLongitude, maxLongitude, longitudeName, longitude) Then
        point = New GeoPoint(latitude, longitude)
        Return True
    End If
    point = Nothing
    Return False
End Function

Private Function TryConvertLocationCoordinate(ByVal str As String, ByVal minValue As Double, ByVal maxValue As Double, ByVal valueName As String, ByRef value As Double) As Boolean
    Dim convertedValue As Double = If(String.IsNullOrEmpty(str), 0, Double.Parse(str))
    If (convertedValue > maxValue) OrElse (convertedValue < minValue) Then
        MessageBox.Show(String.Format(msgMinMaxErrorFormatString, valueName, minValue, maxValue))
        value = 0
        Return False
    End If
    value = convertedValue
    Return True

End Function

Geocode Results

To get the geocode results for a specified location, handle the AzureGeocodeDataProvider.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:

View Example

csharp
private void OnLocationInformationReceived(object sender, LocationInformationReceivedEventArgs e) {
    if(e.Cancelled == true) return;
    if(e.Result.ResultCode != RequestResultCode.Success) {
        meResult.Text = "The Bing Geocode service does not work for this location.";
        return;
    }
    StringBuilder resultList = new StringBuilder("");
    int resCounter = 1;
    foreach(LocationInformation locations in e.Result.Locations) {
        resultList.Append(String.Format("Request Result {0}:\r\n", resCounter));
        resultList.Append(String.Format(locations.EntityType + "\r\n"));
        resultList.Append(String.Format(locations.Address.FormattedAddress + "\r\n"));
        resultList.Append(String.Format("Coordinates: {0}\r\n", locations.Location));
        resultList.Append(String.Format(" ______________________________ \r\n"));
        resCounter++;
    }
    meResult.Text = resultList.ToString();
}
vb
Private Sub OnLocationInformationReceived(ByVal sender As Object, ByVal e As LocationInformationReceivedEventArgs)
    If e.Cancelled = True Then
        Return
    End If
    If e.Result.ResultCode <> RequestResultCode.Success Then
        meResult.Text = "The Bing Geocode service does not work for this location."
        Return
    End If
    Dim resultList As New StringBuilder("")
    Dim resCounter As Integer = 1
    For Each locations As LocationInformation In e.Result.Locations
        resultList.Append(String.Format("Request Result {0}:" & ControlChars.CrLf, resCounter))
        resultList.Append(String.Format(locations.EntityType & ControlChars.CrLf))
        resultList.Append(String.Format(locations.Address.FormattedAddress & ControlChars.CrLf))
        resultList.Append(String.Format("Coordinates: {0}" & ControlChars.CrLf, locations.Location))
        resultList.Append(String.Format(" ______________________________" & ControlChars.CrLf))
        resCounter += 1
    Next locations
    meResult.Text = resultList.ToString()
End Sub

The results for Latitude - “41.27” and Longitude - “-96.05” are shown in the image below.

Examples

The following example demonstrates how to use the geocode feature in the map control:

See Also

Search

Routing