New to Telerik UI for WinUI? 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

        <dataVisualization:RadMap x:Name="myMap" MapMouseClick="bingMap_MapMouseClick"> 
            <dataVisualization:RadMap.Provider> 
                <telerikMap:BingRestMapProvider ApplicationId="Bing_Map_Key"   
                             x:Name="restProvider"  
                             SearchLocationCompleted="restProvider_SearchLocationCompleted"  
                             SearchLocationError="restProvider_SearchLocationError"  
                             Mode="Aerial"/> 
            </dataVisualization:RadMap.Provider> 
        </dataVisualization: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 async void restProvider_SearchLocationCompleted(object sender, BingRestSearchLocationCompletedEventArgs e) 
        { 
            foreach ( var location in e.Locations) 
            { 
                var dialog = new MessageDialog(string.Format("Address: {0}", location.Address.FormattedAddress)); 
                await dialog.ShowAsync(); 
            } 
        } 
        private async void restProvider_SearchLocationError(object sender, BingRestSearchLocationErrorEventArgs e) 
        { 
            var dialog = new MessageDialog(e.Error.ToString()); 
            await dialog.ShowAsync(); 
        } 
    
  • 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 MyMap_Loaded(object sender, Microsoft.UI.Xaml.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 async 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 (var location in e.Locations) 
                { 
                    var coordinates = location.GeocodePoints[0].Coordinates; 
                    double latitude = coordinates[0]; 
                    double longitude = coordinates[1]; 
                    var dialog = new MessageDialog(string.Format("Longitude: {0}, Latitude: {1}, Address: {2}", longitude, latitude, location.Address.FormattedAddress)); 
                    await dialog.ShowAsync(); 
                } 
            } 
     
            private async void restProvider_SearchLocationError(object sender, BingRestSearchLocationErrorEventArgs e) 
            { 
                var dialog = new MessageDialog(e.Error.ToString()); 
                await dialog.ShowAsync(); 
            } 
        }    
    

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.

You can also 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
Not finding the help you need?