Location always returns “Unknown”

霸气de小男生 提交于 2019-12-06 01:54:19

Wait for location services to be ready. Your GeoCoordinateWatcher has an event for status change and another one for position change. Your code should look like this.

//this goes somewhere in your startup sequence
_geoCoordinateWatcher.StatusChanged += 
  new EventHandler<GeoPositionStatusChangedEventArgs>(_gcw_StatusChanged);
_geoCoordinateWatcher.PositionChanged += 
  new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(_gcw_PositionChanged);
_geoCoordinateWatcher.MovementThreshold = 50; //metres
_geoCoordinateWatcher.Start();

...

static void _gcw_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
  if (e.Status == GeoPositionStatus.Ready)
    PhoneApplicationService.Current.State["CurrentLocation"] = 
      _geoCoordinateWatcher.Position.Location;
}

static void _gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
  PhoneApplicationService.Current.State["CurrentLocation"] = e.Position.Location;
}

Since you seem to be having some grief I suggest you start by putting a messagebox in the status change event so you can tell whether it fires on your phone, and once you get that sorted try for position change etc.

Also, have you tried going outside? You may not get a GPS lock inside and cell tower location doesn't always work. Go outside and get clear of tall buildings. If you live in a highrise, go out on a balcony or (best of all) up on the roof.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!