搜索栏 - 地点建议(Bing 地图)Windows Phone App 8.1

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

我想在我的 Windows Phone 应用程序中将搜索添加到 Bing 地图控件。我没有在互联网上找到任何参考资料 设想 1.当用户输入地名时。列表框将打开以显示建议的地点 2. 用户将 select 单个选项,位置或图钉的坐标将显示到该特定区域。 请帮帮我

这很容易完成。您需要一个 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>

当文本更改时,您会发出查询。由于服务器请求需要一些时间,因此请确保选择合理的响应间隔。因此,如果输入刚刚更改了一个字符,请不要发出查询。

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> { };
        }
    }
}

您现在所需要的只是一种发出查询的方法:

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;
}

剩下的很简单:如果用户选择一个建议,您可以从 MapLocation 中获取坐标并在您的地图上显示一个图钉。

这是我为获取邮政编码和城市名称而编写的一些代码。但是你真的可以改变它来搜索其他任何东西..