Back to Devexpress

LocationInformationReceivedEventArgs.Result Property

windowsforms-devexpress-dot-xtramap-dot-locationinformationreceivedeventargs.md

latest11.1 KB
Original Source

LocationInformationReceivedEventArgs.Result Property

Returns the result of a request to obtain information about a specific geographical coordinate.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public GeocodeRequestResult Result { get; }
vb
Public ReadOnly Property Result As GeocodeRequestResult

Property Value

TypeDescription
GeocodeRequestResult

A GeocodeRequestResult object.

|

Example

This example demonstrates how to obtain information about a geographical point from the Bing Geocode web service.

To see how it works, do one of the following:

  • specify a geographical point (GeoPoint.Longitude and GeoPoint.Latitude) in the TextBox elements. Then, click the “Request Location Information” button;
  • find the coordinates of a desired geographical point on the map and click the map point.

After that all parameters are passed to the BingGeocodeDataProvider.RequestLocationInformation method.

Results contain an address (LocationInformation.Address) and exact coordinates (LocationInformation.Location), shown in the RichTextBox element.

View Example

csharp
using System;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace RequestLocationInformation {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        const string yourBingKey = "Your Bing Key Here";
        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;

        BingGeocodeDataProvider geocodeProvider;

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            imageProvider.BingKey = yourBingKey;
            #region #CreateGeocodeProvider
            // Create a geocode data provider.
            geocodeProvider = new BingGeocodeDataProvider {
                BingKey = yourBingKey,
                MaxVisibleResultCount = 1
            };
            geocodeProvider.LocationInformationReceived += OnLocationInformationReceived;
            geocodeProvider.LayerItemsGenerating += OnLayerItemsGenerating;
            informationLayer.DataProvider = geocodeProvider;
            #endregion #CreateGeocodeProvider
        }

        #region #CustomRequestUI
        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;

        }
        #endregion #CustomRequestUI

        #region #CustomRecievedUI
        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();
        }
        #endregion #CustomRecievedUI

        private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e) {
            mapControl.ZoomToFit(e.Items, 0.4);
        }
    }
}
vb
Imports System
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace RequestLocationInformation
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Private Const yourBingKey As String = "Your Bing Key Here"
        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 BingGeocodeDataProvider

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            imageProvider.BingKey = yourBingKey
' #Region "#CreateGeocodeProvider"
            ' Create a geocode data provider.
            geocodeProvider = New BingGeocodeDataProvider With {.BingKey = yourBingKey, .MaxVisibleResultCount = 1}
            AddHandler geocodeProvider.LocationInformationReceived, AddressOf OnLocationInformationReceived
            AddHandler geocodeProvider.LayerItemsGenerating, AddressOf OnLayerItemsGenerating
            informationLayer.DataProvider = geocodeProvider
' #End Region ' #CreateGeocodeProvider
        End Sub

        #Region "#CustomRequestUI"
        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
        #End Region ' #CustomRequestUI

        #Region "#CustomRecievedUI"
        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
        #End Region ' #CustomRecievedUI

        Private Sub OnLayerItemsGenerating(ByVal sender As Object, ByVal e As LayerItemsGeneratingEventArgs)
            mapControl.ZoomToFit(e.Items, 0.4)
        End Sub
    End Class
End Namespace

See Also

LocationInformationReceivedEventArgs Class

LocationInformationReceivedEventArgs Members

DevExpress.XtraMap Namespace