Edit this page

Search Location

The RadMap control allows the user to implement search functionality, that will let the user search for a specific place on the map. The search is performed through an IBingRestSearchLocationProvider interface, which communicates with the respective map provider's services. The BingRestMapProvider implements this interface. In the following sections of this article we will demonstrate how you can implement this functionality.

The RadMap search functionality wraps the Location API of the Bing Rest Map services.

Find a place by given Location or Query

In the following examples, we will demonstrate how to find a specific location by given address or location. To do it you can create an instance of the BingRestSearchLocationRequest object which the SearchLocationAsync() method of the BingRestMapProvider uses as a parameter and set its Query property. Then you can subscribe to the SearchLocationCompleted and SearchLocationError events of the BingRestMapProvider. The SearchLocationError event is the place for handling any kind of errors that occur during the search operation. You can take a look at the following steps:

  • Set the ApplicationId property of the BingRestMapProvider. This property represents a key, which allows you to use the Bing Rest Maps services.

    Without supplying a valid key you won't be able to visualize the map inside the RadMap control. In order to learn how to obtain one, please read Accessing the Control Using a Bing Maps.

  • Create instance of the BingRestSearchLocationRequest object

  • Handle SearchLocationCompleted and SearchLocationError events of the BingRestMapProvider
  • Call SearchLocationAsync() method of the BingRestMapProvider and provide the request as a parameter

The SearchLocationCompleted returns BingRestSearchLocationCompletedEventArgs argument which contains array of Telerik.Windows.Controls.DataVisualization.Map.BingRest.Location objects. This is a complex DataContract object given from the Bing REST services and contains lots of data – address, name, geocode points, district, maneuver type etc. For better overview follow Search sample project.

  • Find a place by Location In this example we will get the location on the MapMouseClick event of the RadMap.

    Example 1: Defining BingRestMapProvider in XAML

        <telerik:RadMap x:Name="myMap" MapMouseClick="bingMap_MapMouseClick">
            <telerik:RadMap.Provider>
                <telerik:BingRestMapProvider ApplicationId="Bing_Key" 
                                             x:Name="restProvider"
                                             SearchLocationCompleted="restProvider_SearchLocationCompleted"
                                             SearchLocationError="restProvider_SearchLocationError"
                                             Mode="Aerial"/>
            </telerik:RadMap.Provider>           
        </telerik:RadMap>
    

    Example 2: Get location by mouse click

        private void bingMap_MapMouseClick(object sender, MapMouseRoutedEventArgs eventArgs)
        {
            GetSearchData(eventArgs.Location);
        }
        private void GetSearchData(Location location)
        {
            // Build location request.
            BingRestSearchLocationRequest request = new BingRestSearchLocationRequest();        
            request.Query = location.ToString();
            // Call the location service.            
            this.restProvider.SearchLocationAsync(request);
        }
        private void restProvider_SearchLocationCompleted(object sender, BingRestSearchLocationCompletedEventArgs e)
        {
            foreach (Telerik.Windows.Controls.DataVisualization.Map.BingRest.Location location in e.Locations)
            {
                MessageBox.Show(string.Format("Address: {0}", location.Address.FormattedAddress));
            }
        }
        private void restProvider_SearchLocationError(object sender, BingRestSearchLocationErrorEventArgs e)
        {
            MessageBox.Show(e.Error.ToString());
        }   
    
        Private Sub myMap_MapMouseClick(sender As Object, eventArgs As MapMouseRoutedEventArgs)
            GetSearchData(eventArgs.Location)
        End Sub
    
        Private Sub GetSearchData(location As Location)
            ' Build location request.'
            Dim data As New BingRestSearchLocationRequest()
            data.Query = location.ToString
            ' Call the location service.'
            Me.restProvider.SearchLocationAsync(data)
        End Sub
    
        Private Sub restProvider_SearchLocationCompleted(sender As Object, e As BingRestSearchLocationCompletedEventArgs)
            For Each location As Telerik.Windows.Controls.DataVisualization.Map.BingRest.Location In e.Locations
                MessageBox.Show(String.Format("Address: {0}", location.Address.FormattedAddress))
            Next
        End Sub
    
        Private Sub restProvider_SearchLocationError(sender As Object, e As BingRestSearchLocationErrorEventArgs)
            MessageBox.Show(e.Error.ToString)
        End Sub
    

  • Find a place by Address/String Query

    The approach here is almost the same as the one explained above. Here we are setting a string address to the Query property of the BingRestSearchLocationRequest object.

    Example 3: Defining BingRestMapProvider in XAML

        <telerik:RadMap x:Name="myMap">
            <telerik:RadMap.Provider>
                <telerik:BingRestMapProvider ApplicationId="Bing_Key" 
                                             x:Name="restProvider"
                                             SearchLocationCompleted="restProvider_SearchLocationCompleted"
                                             SearchLocationError="restProvider_SearchLocationError"
                                             Mode="Aerial"/>
            </telerik:RadMap.Provider>           
        </telerik:RadMap>
    


    Example 3: Get a location by specific address

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.myMap.Loaded += Map_Loaded;
            }
    
            private void Map_Loaded(object sender, RoutedEventArgs e)
            {
                // Build location request.
                BingRestSearchLocationRequest data = new BingRestSearchLocationRequest();
                data.Query = "162 South Avenue, Bloomington, MN 55425";
                // Call the location service.    
                this.restProvider.SearchLocationAsync(data);
            }
    
            private void restProvider_SearchLocationCompleted(object sender, BingRestSearchLocationCompletedEventArgs e)
            {
                double[] bbox = e.Locations[0].BoundingBox;
                LocationRect rect = new LocationRect(new Location(bbox[2], bbox[1]), new Location(bbox[0], bbox[3]));
                this.myMap.SetView(rect);
                foreach (Telerik.Windows.Controls.DataVisualization.Map.BingRest.Location location in e.Locations)
                {
                    var coordinates = location.GeocodePoints[0].Coordinates;
                    double latitude = coordinates[0];
                    double longitude = coordinates[1];
                    MessageBox.Show(string.Format("Longitude: {0}, Latitude: {1}, Address: {2}", longitude, latitude, location.Address.FormattedAddress));
                }
            }
    
            private void restProvider_SearchLocationError(object sender, BingRestSearchLocationErrorEventArgs e)
            {
                MessageBox.Show(e.Error.ToString());
            }
        }   
    
        Class MainWindow
            Public Sub New()
                InitializeComponent()
                AddHandler Me.myMap.Loaded, AddressOf Map_Loaded
            End Sub
    
            Private Sub Map_Loaded(sender As Object, e As RoutedEventArgs)
                ' Build location request.'
                Dim data As New BingRestSearchLocationRequest()
                data.Query = "162 South Avenue, Bloomington, MN 55425"
                ' Call the location service.'
                Me.restProvider.SearchLocationAsync(data)
            End Sub
    
            Private Sub restProvider_SearchLocationCompleted(sender As Object, e As BingRestSearchLocationCompletedEventArgs)
                Dim bbox As Double() = e.Locations(0).BoundingBox
                Dim rect As New LocationRect(New Location(bbox(2), bbox(1)), New Location(bbox(0), bbox(3)))
                Me.myMap.SetView(rect)
                For Each location As Telerik.Windows.Controls.DataVisualization.Map.BingRest.Location In e.Locations
                    Dim coordinates = location.GeocodePoints(0).Coordinates
                    Dim latitude As Double = coordinates(0)
                    Dim longitude As Double = coordinates(1)
                    MessageBox.Show(String.Format("Longitude: {0}, Latitude: {1}, Address: {2}", longitude, latitude, location.Address.FormattedAddress))
                Next
            End Sub
    
            Private Sub restProvider_SearchLocationError(sender As Object, e As BingRestSearchLocationErrorEventArgs)
                MessageBox.Show(e.Error.ToString)
            End Sub
        End Class
    
    ## See Also