AzureGeocodeDataProvider Class
The class that sends requests to the Azure Maps Geocode service.
Namespace: DevExpress.XtraMap
Assembly: DevExpress.XtraMap.v24.2.dll
Declaration
public class AzureGeocodeDataProvider :
AzureMapDataProviderBase,
IMouseClickRequestSender
Remarks
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.
Request Location Point
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);
}
Obtain Request Results
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 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++;
}
memoEdit1.Text = resultList.ToString();
}
private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e) {
mapControl1.ZoomToFit(e.Items, 0.4);
}
}