# Search

> important Please note that [Bing Maps](https://www.bingmapsportal.com/) **will be deprecated effective June 30, 2025**. As an alternative, users can refer to the [SDK example available in our GitHub repository](https://github.com/telerik/winforms-sdk/tree/master/Map/Custom%20Azure%20Provider), which demonstrates how to create a **custom provider** using the **Azure Maps API**. A **valid Azure Maps subscription key** is required to use this functionality.

**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.

> caption Figure 1: Bing search

![WinForms RadMap Bing search](https://www.telerik.com/products/winforms/documentation/assets/1447937bcfa3c1106aab04df974c996a/map-providers-bingmaps-search001.gif)

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

```C#
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;
    
    bingProvider.SearchCompleted += BingProvider_SearchCompleted;
    bingProvider.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!");
    }
}

```

```VB.NET
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 bingProvider.SearchCompleted, AddressOf BingProvider_SearchCompleted
    AddHandler bingProvider.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
```

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

> note 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

```C#
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);
        
```

```VB.NET
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)
```

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

## See Also

* [BingRestMapProvider](https://www.telerik.com/products/winforms/documentation/controls/map/providers/bingmaps/bingrestmapprovider)
