Back to Devexpress

AzureGeocodeDataProvider.LocationInformationReceived Event

windowsforms-devexpress-dot-xtramap-dot-azuregeocodedataprovider-0100d1fa.md

latest13.3 KB
Original Source

AzureGeocodeDataProvider.LocationInformationReceived Event

Occurs when information about the location is received.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public event LocationInformationReceivedEventHandler LocationInformationReceived
vb
Public Event LocationInformationReceived As LocationInformationReceivedEventHandler

Event Data

The LocationInformationReceived event's data class is LocationInformationReceivedEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelledGets a value indicating whether an asynchronous operation has been canceled. Inherited from AsyncCompletedEventArgs.
ErrorGets a value indicating which error occurred during an asynchronous operation. Inherited from AsyncCompletedEventArgs.
ResultReturns the result of a request to obtain information about a specific geographical coordinate.
UserStateGets the unique identifier for the asynchronous task. Inherited from AsyncCompletedEventArgs.

The event data class exposes the following methods:

MethodDescription
RaiseExceptionIfNecessary()Raises a user-supplied exception if an asynchronous operation failed. Inherited from AsyncCompletedEventArgs.

Remarks

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:

csharp
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);
    }
}
vb
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

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the LocationInformationReceived event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

map-for-winforms-azure-geocoding/CS/AzureGeocoding/Form1.cs#L33

csharp
};
geocodeProvider.LocationInformationReceived += OnLocationInformationReceived;
geocodeProvider.LayerItemsGenerating += OnLayerItemsGenerating;

map-for-winforms-azure-geocoding/VB/AzureGeocoding/Form1.vb#L39

vb
geocodeProvider = New AzureGeocodeDataProvider With {.AzureKey = key, .MaxVisibleResultCount = 1}
AddHandler Me.geocodeProvider.LocationInformationReceived, AddressOf OnLocationInformationReceived
AddHandler Me.geocodeProvider.LayerItemsGenerating, AddressOf OnLayerItemsGenerating

See Also

AzureGeocodeDataProvider Class

AzureGeocodeDataProvider Members

DevExpress.XtraMap Namespace