Back to Devexpress

How to: Implement a Custom Geocode Provider

windowsforms-118759-controls-and-libraries-map-control-examples-gis-data-geocoding-how-to-implement-a-custom-geocode-provider.md

latest6.3 KB
Original Source

How to: Implement a Custom Geocode Provider

  • Nov 13, 2018
  • 3 minutes to read

This example shows how to implement a custom geocode provider.

To do this, design a class that inherits the InformationDataProviderBase class and the IMouseClickRequestSender interface, and implement the CreateData and RequestByPoint methods in the class.

Then, design a class that inherits the IInformationData interface and override its IInformationData.OnDataResponse event. Implement the CalculateAddress method to provide custom geocode logic.

csharp
using DevExpress.XtraMap;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomGeocodeProvider {
    public partial class Form1 : Form {
        InformationLayer Layer { get { return (InformationLayer)mapControl1.Layers[1]; } }
        public Form1() {
            InitializeComponent();
            Layer.DataProvider = new GeocodeDataProvider();
        }
    }
        public class GeocodeDataProvider : InformationDataProviderBase, IMouseClickRequestSender {
        public GeocodeDataProvider() {
            this.ProcessMouseEvents = true;
        }
        protected new GeocodeData Data { get { return (GeocodeData)base.Data; } }
        protected override IInformationData CreateData() {
            return new GeocodeData();
        }
        public void RequestByPoint(GeoPoint geoPoint, MapPoint screenPoint) {
            Data.CalculateAddress(geoPoint);
        }
    }
    public class GeocodeData : IInformationData {
        LocationInformation address = new LocationInformation();
        public LocationInformation Address { get { return address; } set { address = value; } }

        public event EventHandler<RequestCompletedEventArgs> OnDataResponse;
        RequestCompletedEventArgs CreateEventArgs() {
            MapItem item = new MapCallout() { Location = address.Location, Text = address.Address.FormattedAddress };
            return new RequestCompletedEventArgs(new MapItem[] { item }, null, false, null);
        }
        protected void RaiseChanged() {
            if (OnDataResponse != null)
                OnDataResponse(this, CreateEventArgs());
        }

        public void CalculateAddress(GeoPoint geoPoint) {
            //Implement your custom geocode logic here
            LocationInformation info = new LocationInformation();
            info.Address = new Address("Address from your service here " + Environment.NewLine + "Coordinates: " + geoPoint.ToString());
            info.Location = new GeoPoint(geoPoint.Latitude, geoPoint.Longitude);
            Address = info;
            //
            RaiseChanged();
        }
    }
    public class Address : AddressBase {
        public Address(string address) {
            this.FormattedAddress = address;
        }
    }
}
vb
Imports DevExpress.XtraMap
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace CustomGeocodeProvider
    Partial Public Class Form1
        Inherits Form

        Private ReadOnly Property Layer() As InformationLayer
            Get
                Return CType(mapControl1.Layers(1), InformationLayer)
            End Get
        End Property
        Public Sub New()
            InitializeComponent()
            Layer.DataProvider = New GeocodeDataProvider()
        End Sub
    End Class
        Public Class GeocodeDataProvider
            Inherits InformationDataProviderBase
            Implements IMouseClickRequestSender

        Public Sub New()
            Me.ProcessMouseEvents = True
        End Sub
        Protected Shadows ReadOnly Property Data() As GeocodeData
            Get
                Return CType(MyBase.Data, GeocodeData)
            End Get
        End Property
        Protected Overrides Function CreateData() As IInformationData
            Return New GeocodeData()
        End Function
        Public Sub RequestByPoint(ByVal geoPoint As GeoPoint, ByVal screenPoint As MapPoint) Implements IMouseClickRequestSender.RequestByPoint

            Data.CalculateAddress(geoPoint)
        End Sub
    End Class
    Public Class GeocodeData
        Implements IInformationData

        Private address_Renamed As New LocationInformation()
        Public Property Address() As LocationInformation
            Get
                Return address_Renamed
            End Get
            Set(ByVal value As LocationInformation)
                address_Renamed = value
            End Set
        End Property

        Public Event OnDataResponse As EventHandler(Of RequestCompletedEventArgs) Implements IInformationData.OnDataResponse

        Private Function CreateEventArgs() As RequestCompletedEventArgs
            Dim item As MapItem = New MapCallout() With { _
                .Location = address_Renamed.Location, _
                .Text = address_Renamed.Address.FormattedAddress _
            }
            Return New RequestCompletedEventArgs(New MapItem() { item }, Nothing, False, Nothing)
        End Function
        Protected Sub RaiseChanged()
            RaiseEvent OnDataResponse(Me, CreateEventArgs())
        End Sub

        Public Sub CalculateAddress(ByVal geoPoint As GeoPoint)
            'Implement your custom geocode logic here
            Dim info As New LocationInformation()
            info.Address = New Address("Address from your service here " & Environment.NewLine & "Coordinates: " & geoPoint.ToString())
            info.Location = New GeoPoint(geoPoint.Latitude, geoPoint.Longitude)
            Address = info
            '
            RaiseChanged()
        End Sub
    End Class
    Public Class Address
        Inherits AddressBase

        Public Sub New(ByVal address As String)
            Me.FormattedAddress = address
        End Sub
    End Class
End Namespace