C# 从Windows Phone 7中的GeoCoordinateWatcher.StatusChanged事件返回位置数据
我正在编写一个Windows Phone应用程序,需要在其中获取用户的位置。我正试图以一种很好的方式(尽我所能)解决这个问题,使用一个单独的类,在其中查询C# 从Windows Phone 7中的GeoCoordinateWatcher.StatusChanged事件返回位置数据,c#,events,windows-phone-7,geolocation,C#,Events,Windows Phone 7,Geolocation,我正在编写一个Windows Phone应用程序,需要在其中获取用户的位置。我正试图以一种很好的方式(尽我所能)解决这个问题,使用一个单独的类,在其中查询geocordinatewatcher位置数据,并将该数据返回给调用方法 问题是,我不知道如何从geocordinatewatcher的StatusChanged事件将LocationData结构返回给调用方法。请参见代码和注释: public struct LocationData { public string latitude;
geocordinatewatcher
位置数据,并将该数据返回给调用方法
问题是,我不知道如何从geocordinatewatcher
的StatusChanged
事件将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
}
}
我不确定这是不是你想要的 注册PositionChanged事件: 您必须添加一个事件侦听器来触发位置的获取
GeoCoordinateWatcher.PositionChanged += GeoCoordinateWatcherPositionChanged;
private void GeoCoordinateWatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var currentLatitude = e.Position.Location.Latitude;
var currentLongitude = e.Position.Location.Longitude;
}
GeoCoordinateWatcher.PositionChanged+=GeoCoordinateWatcher.PositionChanged;
私有void geocordinateWatcherPositionChanged(对象发送器,地理位置更改Deventargs e)
{
var currentLatitude=e.Position.Location.Latitude;
var currentLength=e.Position.Location.Longitude;
}
当位置更改时,on将触发。哦,这就是收到位置时触发的事件…哈哈,谢谢。