从GeoCoordinateWatcher返回位置数据.Windows Phone 7的StatusChanged事件

本文关键字:StatusChanged 事件 Phone Windows GeoCoordinateWatcher 返回 位置 置数据 | 更新日期: 2023-09-27 18:08:49

我正在编写一个Windows Phone应用程序,我需要获得用户的位置。我正试图以一种很好的方式(以及尽可能多地),使用一个单独的类,我正在查询GeoCoordinateWatcher的位置数据,并将该数据返回给调用方法。

问题是,我不知道如何从GeoCoordinateWatcherStatusChanged事件返回LocationData结构到调用方法。参见代码&评论:

public struct LocationData
{
    public string latitude;
    public string longitude;
}

public class LocationService : GeoCoordinateWatcher
{
    private GeoCoordinateWatcher watcher;
    private LocationData StartLocationWatcher()
    {
        LocationData ld = new LocationData();
        // The watcher variable was previously declared as type GeoCoordinateWatcher.
        if (watcher == null)
        {
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        }
        watcher.MovementThreshold = 20; // Use MovementThreshold to ignore noise in the signal.
        watcher.StatusChanged += (o, args) =>
            {
                switch (args.Status)
                {
                    case GeoPositionStatus.Ready:
                       // Use the Position property of the GeoCoordinateWatcher object to get the current location.
                       GeoCoordinate co = watcher.Position.Location;
                       ld.latitude = co.Latitude.ToString("0.000");
                       ld.longitude = co.Longitude.ToString("0.000");
                       //Stop the Location Service to conserve battery power.
                       watcher.Stop();
                       break;
               }
           };
        watcher.Start();
        return ld; //need to return this to the calling method, with latitude and longitude data taken from GeoCoordinateWatcher
    }
}

从GeoCoordinateWatcher返回位置数据.Windows Phone 7的StatusChanged事件

我不确定这是不是你想要的。

注册PositionChanged事件:

你必须添加一个事件监听器来触发获取位置

GeoCoordinateWatcher.PositionChanged += GeoCoordinateWatcherPositionChanged;
private void GeoCoordinateWatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    var currentLatitude = e.Position.Location.Latitude;
    var currentLongitude = e.Position.Location.Longitude;
}

关于PositionChanged何时触发的更多信息