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

Search

RadMap - AzureMapProvider 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 IMapBaseSearchProvider, which communicates with the respective map provider's services. AzureMapProvider implements the IAzureMapSearchProvider interface that inherits the IMapBaseSearchProvider.

Figure 1: Azure search

WinForms RadMap Azure 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 IMapBaseSearchProvider 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 IMapBaseSearchProvider. 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 AzureMapProvider 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.

Azure search


protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    this.radMap1.ShowSearchBar = true;
    AzureMapProvider azureProvider = this.radMap1.Providers[0] as AzureMapProvider;
    Telerik.WinControls.UI.MapLayer pinsLayer = new MapLayer("Pins");
    this.radMap1.Layers.Add(pinsLayer);
    this.radMap1.MapElement.SearchBarElement.SearchProvider = azureProvider;

    azureProvider.SearchCompleted += AzureProvider_SearchCompleted;
    azureProvider.SearchError += AzureProvider_SearchError;
}

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

private void AzureProvider_SearchCompleted(object sender, AzureSearchCompletedEventArgs 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 (Result result in e.AzureResponse.Results)
    {
        var latitude = result.Position.Latitude;
        var longitude = result.Position.Longitude;
        Telerik.WinControls.UI.Map.PointG point = new Telerik.WinControls.UI.Map.PointG(latitude, longitude);
        MapPin pin = new MapPin(point);
        pin.Size = new System.Drawing.Size(20, 40);
        pin.BackColor = Color.Red;
        pin.ToolTipText = result.Address.FreeformAddress;

        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.AzureResponse.Results.Length > 0)
    {
        if (e.AzureResponse.Results.Length == 1)
        {
            var latitude = e.AzureResponse.Results[0].Position.Latitude;
            var longitude = e.AzureResponse.Results[0].Position.Longitude;
            this.radMap1.BringIntoView(new Telerik.WinControls.UI.Map.PointG(latitude, longitude));
        }
        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!");
    }
}


Protected Overrides Sub OnLoad(e As EventArgs)
    MyBase.OnLoad(e)

    Me.radMap1.ShowSearchBar = True
    Dim azureProvider As AzureMapProvider = TryCast(Me.radMap1.Providers(0), AzureMapProvider)
    Dim pinsLayer As New Telerik.WinControls.UI.MapLayer("Pins")
    Me.radMap1.Layers.Add(pinsLayer)
    Me.radMap1.MapElement.SearchBarElement.SearchProvider = azureProvider

    AddHandler azureProvider.SearchCompleted, AddressOf AzureProvider_SearchCompleted
    AddHandler azureProvider.SearchError, AddressOf AzureProvider_SearchError
End Sub

Private Sub AzureProvider_SearchError(sender As Object, e As SearchErrorEventArgs)
    RadMessageBox.Show(e.Error.Message)
End Sub

Private Sub AzureProvider_SearchCompleted(sender As Object, e As AzureSearchCompletedEventArgs)
    Dim allPoints As New Telerik.WinControls.UI.Map.RectangleG(Double.MinValue, Double.MaxValue, Double.MaxValue, Double.MinValue)
    Me.radMap1.Layers("Pins").Clear()

    For Each result As Result In e.AzureResponse.Results
        Dim latitude = result.Position.Latitude
        Dim longitude = result.Position.Longitude
        Dim point As New Telerik.WinControls.UI.Map.PointG(latitude, longitude)
        Dim pin As New MapPin(point)
        pin.Size = New System.Drawing.Size(20, 40)
        pin.BackColor = Color.Red
        pin.ToolTipText = result.Address.FreeformAddress

        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.AzureResponse.Results.Length > 0 Then
        If e.AzureResponse.Results.Length = 1 Then
            Dim latitude = e.AzureResponse.Results(0).Position.Latitude
            Dim longitude = e.AzureResponse.Results(0).Position.Longitude
            Me.radMap1.BringIntoView(New Telerik.WinControls.UI.Map.PointG(latitude, longitude))
        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 IMapBaseSearchProvider 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.

Azure Search Request

You can perform searching programmatically by using a AzureSearchRequest:


AzureSearchRequest request = new AzureSearchRequest();
request.Query = "San Marino";
request.SearchOptions.Count = 10;
AzureMapProvider azureMapProvider = this.radMap1.Providers[0] as AzureMapProvider;
azureMapProvider.SearchAsync(request);

Dim request As New AzureSearchRequest()
request.Query = "San Marino"
request.SearchOptions.Count = 10
Dim azureMapProvider As AzureMapProvider = TryCast(Me.radMap1.Providers(0), AzureMapProvider)
azureMapProvider.SearchAsync(request)


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

See Also

In this article