BingGeocodeDataProvider.LocationInformationReceived Event

Occurs when information about the location has been received.

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v20.1.dll

Declaration

public event LocationInformationReceivedEventHandler LocationInformationReceived
Public Event LocationInformationReceived As LocationInformationReceivedEventHandler

Event Data

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

Property Description
Cancelled Gets a value indicating whether an asynchronous operation has been canceled. Inherited from AsyncCompletedEventArgs.
Error Gets a value indicating which error occurred during an asynchronous operation. Inherited from AsyncCompletedEventArgs.
Result Returns the result of a request to obtain information about a specific geographical coordinate.
UserState Gets the unique identifier for the asynchronous task. Inherited from AsyncCompletedEventArgs.

Examples

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.

Note that if you run this sample as is, you will get a warning message informing you that the specified Bing Maps key is invalid. To learn more about how to register a Bing Maps account and create a key for it, refer to the How to: Get a Bing Maps Key tutorial.

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);
        }
    }
}
See Also