windowsforms-devexpress-dot-xtramap-fca654d0.md
The class that sends requests to the Azure Maps Geocode service.
Namespace : DevExpress.XtraMap
Assembly : DevExpress.XtraMap.v25.2.dll
NuGet Package : DevExpress.Win.Map
public class AzureGeocodeDataProvider :
AzureMapDataProviderBase,
IMouseClickRequestSender
Public Class AzureGeocodeDataProvider
Inherits AzureMapDataProviderBase
Implements IMouseClickRequestSender
The map control allows you to search for a full or partial address using the Azure Maps Search service and return the longitude and latitude coordinates of the address. This process is called geocoding. The ability to geocode in a country/region depends on the availability of road data and the precision of the geocoding service. For more information about Azure Maps’ geocoding capabilities by country or region, see Geocoding coverage.
The AzureGeocodeDataProvider implements the Azure Maps Geocode service. Assign this object to the InformationLayer.DataProvider property to enable the service and allow users to obtain information about a point on a map.
Azure Maps services support JSON response formats. Install the System.Text.Json package in projects that target .NET Framework to parse the Azure server response and display information on a DevExpress Map control.
Call the RequestLocationInformation method overloads to obtain information about the specified location.
The following example searches for a geographical point according to the specified coordinates:
using DevExpress.XtraMap;
// ...
const string key = "your key";
// ...
private void Form1_Load(object sender, EventArgs e) {
AzureGeocodeDataProvider geocodeProvider = new AzureGeocodeDataProvider {
AzureKey = key
};
geocodeProvider.RequestLocationInformation(new GeoPoint(40.730610, -73.935242));
// Create a layer.
InformationLayer informationLayer = new InformationLayer();
informationLayer.DataProvider = geocodeProvider;
// Add the created layer to the collection.
map.Layers.Add(informationLayer);
}
Imports DevExpress.XtraMap
' ...
Private Const key As String = "your key"
' ...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim geocodeProvider As AzureGeocodeDataProvider = New AzureGeocodeDataProvider With {.AzureKey = key}
geocodeProvider.RequestLocationInformation(new GeoPoint(40.730610, -73.935242))
' Create a layer.
Dim informationLayer As New InformationLayer()
informationLayer.DataProvider = geocodeProvider
' Add the created layer to the collection.
map.Layers.Add(informationLayer)
End Sub
To get geocode results, handle the LocationInformationReceived event. The e.Result argument returns a GeocodeRequestResult descendant that stores geocode results.
If you wish to specify the number of requested results displayed, use the InformationDataProviderBase.MaxVisibleResultCount property.
This example demonstrates how to obtain information about a geographical point from the Azure Geocode service.
TextEdit elements specify a geographical point (GeoPoint.Longitude and GeoPoint.Latitude). The “Request Location Information” button initiates the geocoding request. It obtains the point information and passes it to the BingGeocodeDataProvider.RequestLocationInformation method.
Results contain an address (LocationInformation.Address) and exact coordinates (LocationInformation.Location), shown in the MemoEdit element.
The following image illustrates the result:
using DevExpress.XtraMap;
using System.Text;
// ...
public partial class Form1 : Form {
const string key = "your key";
const string msgMinMaxErrorFormatString = "The {0} must be less than or equal to {2} and greater than or equal to {1}. Correct the input value.";
const string latitudeName = "Latitude";
const double minLatitude = -90;
const double maxLatitude = 90;
const string longitudeName = "Longitude";
const double minLongitude = -180;
const double maxLongitude = 180;
AzureGeocodeDataProvider geocodeProvider;
AzureMapDataProvider imageProvider;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
imageProvider = new AzureMapDataProvider {
AzureKey = key
};
// Create a geocode data provider.
geocodeProvider = new AzureGeocodeDataProvider {
AzureKey = key,
MaxVisibleResultCount = 1
};
geocodeProvider.LocationInformationReceived += OnLocationInformationReceived;
geocodeProvider.LayerItemsGenerating += OnLayerItemsGenerating;
simpleButton1.Click += requestLocation_Click;
informationLayer.DataProvider = geocodeProvider;
imageLayer.DataProvider = imageProvider;
}
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(textEdit1.Text, minLatitude, maxLatitude, latitudeName, out latitude) &&
TryConvertLocationCoordinate(textEdit2.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;
}
private void OnLocationInformationReceived(object sender, LocationInformationReceivedEventArgs e) {
if (e.Cancelled == true) return;
if (e.Result.ResultCode != RequestResultCode.Success) {
memoEdit1.Text = "The Azure Maps 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++;
}
memoEdit1.Text = resultList.ToString();
}
private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e) {
mapControl1.ZoomToFit(e.Items, 0.4);
}
}
Imports DevExpress.XtraMap
Imports System.Text
' ...
Partial Public Class Form1
Inherits Form
Private Const key As String = "your key"
Private Const msgMinMaxErrorFormatString As String = "The {0} must be less than or equal to {2} and greater than or equal to {1}. Correct the input value."
Private Const latitudeName As String = "Latitude"
Private Const minLatitude As Double = -90
Private Const maxLatitude As Double = 90
Private Const longitudeName As String = "Longitude"
Private Const minLongitude As Double = -180
Private Const maxLongitude As Double = 180
Private geocodeProvider As AzureGeocodeDataProvider
Private imageProvider As AzureMapDataProvider
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
imageProvider = New AzureMapDataProvider With {.AzureKey = key}
' Create a geocode data provider.
geocodeProvider = New AzureGeocodeDataProvider With {
.AzureKey = key,
.MaxVisibleResultCount = 1
}
AddHandler geocodeProvider.LocationInformationReceived, AddressOf OnLocationInformationReceived
AddHandler geocodeProvider.LayerItemsGenerating, AddressOf OnLayerItemsGenerating
AddHandler simpleButton1.Click, AddressOf requestLocation_Click
informationLayer.DataProvider = geocodeProvider
imageLayer.DataProvider = imageProvider
End Sub
Private Sub requestLocation_Click(ByVal sender As Object, ByVal e As System.EventArgs)
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(textEdit1.Text, minLatitude, maxLatitude, latitudeName, latitude) AndAlso TryConvertLocationCoordinate(textEdit2.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
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
memoEdit1.Text = "The Azure Maps 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}:" & vbCrLf, resCounter))
resultList.Append(String.Format(locations.EntityType & vbCrLf))
resultList.Append(String.Format(locations.Address.FormattedAddress & vbCrLf))
resultList.Append(String.Format("Coordinates: {0}" & vbCrLf, locations.Location))
resultList.Append(String.Format(" ______________________________" & vbCrLf))
resCounter += 1
Next locations
memoEdit1.Text = resultList.ToString()
End Sub
Private Sub OnLayerItemsGenerating(ByVal sender As Object, ByVal e As LayerItemsGeneratingEventArgs)
mapControl1.ZoomToFit(e.Items, 0.4)
End Sub
End Class
Object InformationDataProviderBase WebInformationDataProvider AzureMapDataProviderBase AzureGeocodeDataProvider
See Also