我有以下代码可以正常工作,除了它提供了重新启动应用程序时跟踪的最后位置详细信息。
在我点击btnGetLocationDetails按钮2或3次后,它会给出正确的位置详细信息。 任何想法如何解决这个问题,以便每次启动应用程序时,用户不需要点击此按钮2或3次以获得正确的当前位置?
代码:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher = null;
Location location=null;
private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.Permission == GeoPositionPermission.Granted)
{
watcher.MovementThreshold = Convert.ToDouble("100");
}
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);
// PositionChanged events occur whenever your position changes
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//get the coordinates
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
location.Altitude = e.Position.Location.Altitude;
}
void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled)
MessageBox.Show("The location service is turned off.");
else if (e.Status == GeoPositionStatus.NoData)
MessageBox.Show("No location data is available. ");
}
}