WP - 如何不断更新我的位置

时间:2013-05-07 11:52:15

标签: mvvm windows-phone mvvm-light

当我通过模拟器将其更改为其他位置时,我的位置不会更新。但是在重新启动我的应用程序后它会改变。这是我在应用启动时编写的内容

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;
        GeoCoordinate DefaultLocation = new GeoCoordinate(-6.595139, 106.793801);
        Library.GPSServices MyGPS;

        if (!Settings.Contains("FirstLaunch") || (bool)Settings["FirstLaunch"] == true)
        {
            Settings["FirstLaunch"] = false;
            Settings["LastLocation"] = DefaultLocation;
            Settings["SearchRadius"] = 1;
        }

        //If key not exist OR key value was set to false, ask for permission to use location
        if (!Settings.Contains("LocationService") || (bool)Settings["LocationService"] == false)
        {
            var result = MessageBox.Show(
                "Jendela Bogor need to know your location to work correctly, do you want to allow it?",
                "Allow access to your location?",
                MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                Settings["LocationService"] = true;
                MyGPS = new Library.GPSServices();
            }
            else
            {
                Settings["LocationService"] = false;
            }

            Settings.Save();
        }

        else if ((bool)Settings["LocationService"] == true)
        {
            MyGPS = new Library.GPSServices();
        }
    }

我将我的位置存储在名为IsolatedStorage

的应用设置Settings["LastLocation"]

如何使用MVVM模式(MVVM-Light)在后台不断更新我的位置,以便在ThirdPageView中的地图上PushPin始终更新?

修改

   public GPSServices()
    {
        if ((bool)Settings["LocationService"] == true)
        {          
            if (_watcher == null)
            {
                _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                _watcher.MovementThreshold = 20;
            }

            StartWatcher();

            _watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        }
        else if ((bool)Settings["LocationService"] == false)
        {
            StopWatcher();
        }
    }

    private void StartWatcher()
    {
        _watcher.Start();
    }

    private void StopWatcher()
    {
        if (_watcher != null)
            _watcher.Stop();
    }

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

        Settings["LastLocation"] = e.Position.Location;
        Settings.Save();
    }

2 个答案:

答案 0 :(得分:0)

System.Device.Location.GeoCoordinateWatcher提供您所需要的内容。

var geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
// This event fires every time the device location changes
geoWatcher.PositionChanged += (s, e) => {  
    //e.Position.Location will contain the current GeoCoordinate
};

geoWatcher.TryStart(false, TimeSpan.FromMilliseconds(2000));

答案 1 :(得分:0)

这对你有帮助吗?使用GeocoordinateWatcher.PositionChanged事件?

public Location()
    {
        GeoCoordinateWatcher location == new GeoCoordinateWatcher();
        location.PositionChanged += new  EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(location_PositionChanged);
        location.Start();
    }


    //event to track the location change
    public void location_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {

    }