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

Search

RadMap allows you to implement search functionality, that will let the user search for a specific location on the map. The search is performed through an IMapSearchProvider, which communicates with the respective map provider's services. BingRestMapProvider implements the IMapSearchProvider interface.

Figure 1: Bing search

WinForms RadMap Bing search

In order to use the built-in search bar that RadMap offers set the ShowSearchBar property to true. Depending on the amount of control you want to have over the search you can do one of three things. First, you can subscribe to the MapSearchBarElement's SearchCriteriaChanged event. In the event handler you will receive the search criteria and you are responsible for the rest of the search handling. That is RadMap will not perform any further actions related to the search. Second, if you have not subscribed to the SearchCriteriaChanged event you can set the SearchBarElement.SearchProvider property to an IMapSearchProvider instance. When you do this RadMap will initiate a new search through that provider. You should still handle the search result through the provider's SearchCompleted and SearchError events. Third, if you have not subscribed to the event and you have not set the SearchBarElement.SearchProvider property RadMap will go through its Providers collection and will initiate a search through the first provider that implements the IMapSearchProvider. Results will be displayed in a layer named Search. If the layer does not exist it will be added by RadMap. You should note that all overlays will be removed from the Search layer when a new search is initiated.

In this example we will use the second approach by setting the MapElement.SearchBarElement.SearchProvider property to the BingRestMapProvider that the map is currently using. Then, subscribe to the SearchProvider's SearchCompleted and SearchError events. The SearchCompletedEventArgs gives you the Locations result which can be used to place a MapPin to the added pins MapLayer for each search result. The SearchError event is the place for handling any kind of errors that occur during the search operation.

Bing search


private void BingProvider_Load(object sender, EventArgs e)
{
    this.radMap1.ShowSearchBar = true;
    BingRestMapProvider bingProvider = this.radMap1.Providers[0] as BingRestMapProvider;
    Telerik.WinControls.UI.MapLayer pinsLayer = new MapLayer("Pins");
    this.radMap1.Layers.Add(pinsLayer);
    this.radMap1.MapElement.SearchBarElement.SearchProvider = bingProvider;

    this.radMap1.MapElement.SearchBarElement.SearchProvider.SearchCompleted += BingProvider_SearchCompleted;
    this.radMap1.MapElement.SearchBarElement.SearchProvider.SearchError += BingProvider_SearchError;
}

private void BingProvider_SearchError(object sender, SearchErrorEventArgs e)
{
    RadMessageBox.Show(e.Error.Message);
}

private void BingProvider_SearchCompleted(object sender, SearchCompletedEventArgs e)
{
    Telerik.WinControls.UI.Map.RectangleG allPoints = new Telerik.WinControls.UI.Map.RectangleG(double.MinValue, double.MaxValue, double.MaxValue, double.MinValue);
    this.radMap1.Layers["Pins"].Clear();
    foreach (Telerik.WinControls.UI.Map.Bing.Location location in e.Locations)
    {
        Telerik.WinControls.UI.Map.PointG point = new Telerik.WinControls.UI.Map.PointG(location.Point.Coordinates[0], location.Point.Coordinates[1]);
        MapPin pin = new MapPin(point);
        pin.Size = new System.Drawing.Size(20, 40);
        pin.BackColor = Color.Red;
        pin.ToolTipText = location.Address.FormattedAddress;
        this.radMap1.MapElement.Layers["Pins"].Add(pin);
        allPoints.North = Math.Max(allPoints.North, point.Latitude);
        allPoints.South = Math.Min(allPoints.South, point.Latitude);
        allPoints.West = Math.Min(allPoints.West, point.Longitude);
        allPoints.East = Math.Max(allPoints.East, point.Longitude);
    }
    if (e.Locations.Length > 0)
    {
        if (e.Locations.Length == 1)
        {
            this.radMap1.BringIntoView(new Telerik.WinControls.UI.Map.PointG(e.Locations[0].Point.Coordinates[0], e.Locations[0].Point.Coordinates[1]));
        }
        else
        {
            this.radMap1.MapElement.BringIntoView(allPoints);
            this.radMap1.Zoom(this.radMap1.MapElement.ZoomLevel - 1);
        }
    }
    else
    {
        RadMessageBox.Show("No result found for the provided search query!");
    }
}

Private Sub BingProvider_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.radMap1.ShowSearchBar = True
    Dim bingProvider As BingRestMapProvider = TryCast(Me.radMap1.Providers(0), BingRestMapProvider)
    Dim pinsLayer As Telerik.WinControls.UI.MapLayer = New MapLayer("Pins")
    Me.radMap1.Layers.Add(pinsLayer)
    Me.radMap1.MapElement.SearchBarElement.SearchProvider = bingProvider
    AddHandler Me.radMap1.MapElement.SearchBarElement.SearchProvider.SearchCompleted, AddressOf BingProvider_SearchCompleted
    AddHandler Me.radMap1.MapElement.SearchBarElement.SearchProvider.SearchError, AddressOf BingProvider_SearchError
End Sub
Private Sub BingProvider_SearchError(sender As Object, e As SearchErrorEventArgs)
    RadMessageBox.Show(e.[Error].Message)
End Sub
Private Sub BingProvider_SearchCompleted(sender As Object, e As SearchCompletedEventArgs)
    Dim allPoints As New Telerik.WinControls.UI.Map.RectangleG(Double.MinValue, Double.MaxValue, Double.MaxValue, Double.MinValue)
    Me.radMap1.Layers("Pins").Clear()
    For Each location As Telerik.WinControls.UI.Map.Bing.Location In e.Locations
        Dim point As New Telerik.WinControls.UI.Map.PointG(location.Point.Coordinates(0), location.Point.Coordinates(1))
        Dim pin As New MapPin(point)
        pin.Size = New System.Drawing.Size(20, 40)
        pin.BackColor = Color.Red
        pin.ToolTipText = location.Address.FormattedAddress
        Me.radMap1.MapElement.Layers("Pins").Add(pin)
        allPoints.North = Math.Max(allPoints.North, point.Latitude)
        allPoints.South = Math.Min(allPoints.South, point.Latitude)
        allPoints.West = Math.Min(allPoints.West, point.Longitude)
        allPoints.East = Math.Max(allPoints.East, point.Longitude)
    Next
    If e.Locations.Length > 0 Then
        If e.Locations.Length = 1 Then
            Me.radMap1.BringIntoView(New Telerik.WinControls.UI.Map.PointG(e.Locations(0).Point.Coordinates(0), e.Locations(0).Point.Coordinates(1)))
        Else
            Me.radMap1.MapElement.BringIntoView(allPoints)
            Me.radMap1.Zoom(Me.radMap1.MapElement.ZoomLevel - 1)
        End If
    Else
        RadMessageBox.Show("No result found for the provided search query!")
    End If
End Sub

Enter some text in the search box and press Enter to start searching.

The MapSearchBarElement can be setup with no explicit search provider. In this case the search element will traverse the providers collection of RadMapElement for a IMapSearchProvider instance. The MapSearchBarElement.ShowMessageBoxOnError property determines whether a messagebox will be displayed upon error or not when the SearchProvider property of the element is not set. The default value of the property is set to true.

SearchRequest

You can perform searching programmatically by using a SearchRequest:

Bing search


Telerik.WinControls.UI.Map.Bing.SearchRequest request = new SearchRequest();
request.Query = "San Marino";
request.SearchOptions.Count = 10;
request.SearchOptions.QueryParse = true;
BingRestMapProvider bingProvider = this.radMap1.Providers[0] as BingRestMapProvider;
bingProvider.SearchAsync(request);

Dim request As Telerik.WinControls.UI.Map.Bing.SearchRequest = New SearchRequest()
request.Query = "San Marino"
request.SearchOptions.Count = 10
request.SearchOptions.QueryParse = True
Dim bingProvider As BingRestMapProvider = TryCast(Me.radMap1.Providers(0), BingRestMapProvider)
bingProvider.SearchAsync(request)

It is necessary to handle the SearchProvider's SearchCompleted and SearchError events as it is demonstrated above.

See Also

In this article