[英]Windows phone 8 Geolocator Exception
Hi I am new to WP development. 嗨,我是WP开发的新手。 I want to find the users current zipcode or postal code. 我想找到用户当前的邮政编码或邮政编码。 So I have the following code in place. 所以我有以下代码。 Problem is this line d.zip = position.CivicAddress.PostalCode;
问题是这行d.zip = position.CivicAddress.PostalCode;
always gives an object reference not set to an instance of an object exception.But I am able to get the latitude and longitude just fine. 总是给对象引用未设置为对象异常的实例。但是我能够很好地获得纬度和经度。 I have also tried position.CivicAddresss.City but still the same exception. 我也尝试过position.CivicAddresss.City,但仍然是相同的例外。 Please help. 请帮忙。
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; }
}
I believe GeoPosition.CivicAddress is deprecated. 我相信GeoPosition.CivicAddress已弃用。 You need to use the MapLocationFinder to get address information. 您需要使用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.