Sameed Tariq
Sameed Tariq

Reputation: 81

Search Bar - Suggestoin of Places (Bing Map) Windows Phone App 8.1

I want to add Search to Bing Map Control in my Windows Phone app. I did not find any references on the internet Scenario 1. When the user enters the name of the place. The List Box will open to show the suggested places 2. The User will select the a single option and the coordinates of the location or pushpin will be displayed to that particular area. Help me Out Please

Upvotes: 1

Views: 596

Answers (1)

Fred
Fred

Reputation: 3362

It's fairly easy to accomplish. You need an AutoSuggestBox:

<AutoSuggestBox TextChanged="AutoSuggestBox_TextChanged" SuggestionChosen="AutoSuggestBox_SuggestionChosen">
        <AutoSuggestBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Address.PostCode}" />
                    <TextBlock Text="," />
                    <TextBlock Text="{Binding Path=Address.Town}" />
                </StackPanel>
            </DataTemplate>
        </AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>

And when the text changes you issue a query. Since a server request takes some time, make sure that you choose a reasonable response interval. So DO NOT issue a query if the input has just changed for one char.

private async void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
    {
        if (sender.Text.Length > 3)
        {
            // Build a hint point
        Geopoint hintPoint = [..]; // Up to you, should reflect the current location
            await this.Progressbar.ShowAsync();
            sender.ItemsSource = await getMapSuggestionsAsync(sender.Text, hintPoint);
            await this.Progressbar.HideAsync();
        }
        else {
            sender.ItemsSource = new List<MapLocation> { };
        }
    }
}

All you need now is a method that issues your query:

public static async Task<List<MapLocation>> getMapSuggestionsAsync(String query, Geopoint hintPoint)
{
    List<MapLocation> locations = new List<MapLocation>();
    // Find a corresponding location
    MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(query, hintPoint, 2);

    // If the query provides results, try to get the respective city name and
    // a zip code or deny first.
    if (result.Status != MapLocationFinderStatus.Success)
        return locations;
    foreach (var location in result.Locations)
    {
        MapLocation ml = await resolveLocationForGeopoint(location.Point);
        if (ml != null)
            locations.Add(ml);
    }
    return locations;
}

public static async Task<MapLocation> resolveLocationForGeopoint(Geopoint geopoint)
{
    MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(geopoint);
    if (result.Status == MapLocationFinderStatus.Success)
    {
        if (result.Locations.Count != 0)
            // Check if the result is really valid
            if (result.Locations[0].Address.Town != "")
                return result.Locations[0];
    }
    return null;
}

The rest is simple: If the user chooses a suggestion you get the coordinates out of the MapLocation and show a pushpin on your map.

This is some code I wrote to get a Zip code and the city name. But you can really change it to search for anything else..

Upvotes: 2

Related Questions