【发布时间】:2015-10-19 21:37:13
【问题描述】:
我有一个按钮,只要按下它就会调用CurrentLocation。
private async void CurrentLocation()
{
try
{
Geolocator myGeolocator = new Geolocator();
Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(3));
Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
this.MyMap.Center = myGeoCoordinate;
}
catch
{ }
}
我一直在 Windows Phone 模拟器上测试该应用程序,一切正常。但是今天,当我在开车时按下 Lumina 640 上运行的应用程序中的按钮时,应用程序开始显示不同的位置。
有人知道我的代码有什么问题吗?
编辑:
构造器
public MainPage()
{
InitializeComponent();
distanceTextBox.Visibility = Visibility.Collapsed;
CreateStandardApplicationBar();
pointNamePrompt = new InputPrompt()
{
Title = "Point",
Message = "Name the point",
};
try
{
CurrentLocation();
MyMap.ZoomLevel = 10;
}
catch { }
LoadAppSettings();
}
按钮
private void CurrentLocation_Click(object sender, EventArgs e)
{
CurrentLocation();
}
最后是新的新代码,它在应用启动时第一次仍然有效:
private async void CurrentLocation()
{
try
{
Geolocator myGeolocator = new Geolocator();
Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10));
ReverseGeocodeQuery query = new ReverseGeocodeQuery();
query.GeoCoordinate = new System.Device.Location.GeoCoordinate(myGeoposition.Coordinate.Latitude, myGeoposition.Coordinate.Longitude);
query.QueryCompleted += (s, e) =>
{
if (e.Error != null && e.Result.Count == 0)
return;
MessageBox.Show(e.Result[0].Information.Address.PostalCode);
};
query.QueryAsync();
double lat = 0.00, lng = 0.00;
lat = Convert.ToDouble(myGeoposition.Coordinate.Latitude);
lng = Convert.ToDouble(myGeoposition.Coordinate.Longitude);
this.MyMap.Center = new GeoCoordinate(lat, lng);
}
catch
{ }
}
【问题讨论】:
标签: c# windows-phone-8 location