如何在wp8中按地址获取位置并在AutoCompleteBox中显示
本文关键字:位置 AutoCompleteBox 显示 获取 wp8 地址 | 更新日期: 2023-09-27 18:11:17
我正在开发wp8应用。我想创建AutoCompleteBox快速搜索。这是我的xaml:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="ContentPanel" Grid.Row="0" Grid.RowSpan="2">
<maps:Map x:Name="MapWithMyLocation" Tap="mapWithMyLocation_Tap">
</maps:Map>
</Grid>
<Grid Grid.Row="0">
<controls:AutoCompleteBox x:Name="TxtSearch" TextChanged="TxtSearch_OnTextChanged" />
</Grid>
</Grid>
这里是AutoCompleteBox上的事件TxtSearch_OnTextChanged
private void TxtSearch_OnTextChanged(object sender, RoutedEventArgs e)
{
if (TxtSearch.Text.Length > 0)
{
Maps_GeoCoding(TxtSearch.Text);
}
}
private async void Maps_GeoCoding(string sender)
{
var myGeolocator = new Geolocator();
var myGeoposition = await myGeolocator.GetGeopositionAsync();
var myGeocoordinate = myGeoposition.Coordinate;
MyGeoCoordinate =
CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
if (MyGeoCoordinate == null) return;
var geoQuery = new GeocodeQuery { SearchTerm = sender, GeoCoordinate = MyGeoCoordinate };
var locations = await geoQuery.GetMapLocationsAsync();
var items = new List<String>();
var str = "";
foreach (var item in locations)
{
str += item.Information.Address.City + " ";
str += item.Information.Address.Street + " ";
str += item.Information.Address.HouseNumber + " ";
items.Add(str);
str = "";
}
TxtSearch.ItemsSource = items;
}
但是AutoCompleteBox显示一些错误的信息或不显示。什么好主意吗?
刚刚重写了我的函数。现在我使用FirstOrDefault只获得一个项目。下面是示例:
private async void Maps_GeoCoding(string sender)
{
var myAddress = new List<String>();
var myGeolocator = new Geolocator();
var myGeoposition = await myGeolocator.GetGeopositionAsync();
var myGeocoordinate = myGeoposition.Coordinate;
MyGeoCoordinate =
CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
if (MyGeoCoordinate == null) return;
var geoQuery = new GeocodeQuery { SearchTerm = sender, GeoCoordinate = MyGeoCoordinate };
var locations = await geoQuery.GetMapLocationsAsync();
var location = locations.FirstOrDefault();
if (location != null)
{
myAddress.Add(location.Information.Address.City + " " + location.Information.Address.Street + " " + location.Information.Address.HouseNumber);
MapWithMyLocation.Center = location.GeoCoordinate;
}
TxtSearch.ItemsSource = myAddress;
}
XAML与问题相同。