迁移到WP8后,GeoCoordinateWatcher无法正常工作

时间:2013-02-17 15:01:19

标签: windows-phone-7 geolocation windows-phone-8

我为Windows Phone 7.1开发了一个导航应用程序。它在那里运行良好。更新到8.0后,我的GeoCoordinateWatcher不再工作了。我知道我可以使用Geolocator,但由于时间不够,我拒绝这样做。

对于我的应用程序,我读取了我的观察者的当前位置,以将其存储为具有位置信息的对象实例。保存对象实例时,经度和纬度为0.0。即使我在我的模拟器中更改位置仍为0.0。在我使用GeoCoordinateWatcher的其他页面上也会出现同样的问题。它不起作用。正如我已经说过的那样,在WP 7.1-7.8中,它的效果非常好。

    public Map()
    {
        InitializeComponent();

        watcher = new GeoCoordinateWatcher();
        watcher.MovementThreshold = 20;
        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
    }

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        if (e.Position.Location.IsUnknown)
        {
            MessageBox.Show("Please wait while your prosition is determined.");
            return;
        }

        geo.Latitude = e.Position.Location.Latitude;
        geo.Longitude = e.Position.Location.Longitude;
    }

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    GeoCoordinateWatcher watch;
public GeoCoordinate loc = null;
public MainPage()
{
    InitializeComponent();
    if (watch == null)
    {
        watch = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
        {
            MovementThreshold=10
        };
        watch.Start();
        watch.PositionChanged += watch_PositionChanged;
    }
}
void watch_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    //throw new NotImplementedException();
    Dispatcher.BeginInvoke(()=>LocUpdate(e));
}

void LocUpdate(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    try
    {
        location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);


    }
    catch
    {
        MessageBox.Show("Error");
    }
}