New to Telerik UI for WPF? Download free 30-day trial

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 
    

Structured / Unstructured URL

The Microsoft Bing Maps REST Service provides two URL templates which you can use to get latitude and longitude coordinates for a location by a given query. To learn more about these templates, please refer to the Find a Location by Address) MSDN article.

With the R1 2018 SP2 version of our controls, you can now change the type of URL which the BingRestMapProvider's search mechanism uses in its code. By default, the BingRestSearchLocationRequest uses the structured URL template. To force the search functionality to use an unstructured URL template, you can set the UseUnstructuredQuery property of the BingRestSearchLocationRequest to True.

Setting one of these URLs depends on how you have structured your query. To search for a location you can type it in structured form, for example, '1 Microsoft way, Redmond WA 98052'. In such a scenario you can use a Structured URL. On the other hand, if you type an unstructured address (or free form query) you should choose the unstructured Bing URL to get better results from the service.

Example 4: Get a location by specific address

    BingRestMapProvider provider = new BingRestMapProvider(MapMode.Aerial, true, "Your Bing Map Key"); 
    BingRestSearchLocationRequest request = new BingRestSearchLocationRequest(); 
    request.SearchLocationOptions.UseUnstructuredQuery = true; 
    request.Query = "Your query"; 
    this.provider.SearchLocationAsync(request);      

See Also

In this article