[英]Windows phone 8 Geolocator Exception
嗨,我是WP開發的新手。 我想找到用戶當前的郵政編碼或郵政編碼。 所以我有以下代碼。 問題是這行d.zip = position.CivicAddress.PostalCode;
總是給對象引用未設置為對象異常的實例。但是我能夠很好地獲得緯度和經度。 我也嘗試過position.CivicAddresss.City,但仍然是相同的例外。 請幫忙。
async void Button_Click_1(object sender, RoutedEventArgs e)
{
Data d = new Data();
Geolocator locator = new Geolocator();
try
{
Geoposition position = await locator.GetGeopositionAsync();
d.zip = position.CivicAddress.PostalCode;
d.Latitude = position.Coordinate.Latitude;
d.Longitude = position.Coordinate.Longitude;
MessageBox.Show(d.zip);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class Data
{
public string zip { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
我相信GeoPosition.CivicAddress已棄用。 您需要使用MapLocationFinder來獲取地址信息。
async void Button_Click_1(object sender, RoutedEventArgs e)
{
Data d = new Data();
Geolocator locator = new Geolocator();
try
{
Geoposition position = await locator.GetGeopositionAsync();
// Get address data with MapLocationFinder
var result = await MapLocationFinder.FindLocationsAtAsync(position.Coordinate.Point);
if (result.Status == MapLocationFinderStatus.Success)
{
var address = result.Locations[0].Address;
var zip = address.PostCode;
d.zip = zip;
}
// These are deprecated as well.
// Use position.Coordinate.Point.Position.Latitude and
// position.Coordinate.Point.Position.Longitude
d.Latitude = position.Coordinate.Latitude;
d.Longitude = position.Coordinate.Longitude;
MessageBox.Show(d.zip);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class Data
{
public string zip { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.