位置始终返回“未知” [英] Location always returns "Unknown"

查看:128
本文介绍了位置始终返回“未知”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在真实设备上尝试了我的第一个WP7应用程序。我的问题是我使用地理定位功能之一,但我总是得到位置未知。
我不知道是否有任何方法将位置权限授予我的应用程序,或者我缺少某些东西。
在手机设置中,位置服务已启用,并且地图应用在查找我的实际位置时没有任何问题。
我检查了GeoCoordinateWatcher.Permisson属性,其值为已授予。
我已经在WMAppManifest.xml中有这一行< Capability Name =ID_CAP_LOCATION/>



有什么想法可以解决它?



这是我的代码。我在你告诉我这样做后添加了开始行,但我仍然有问题。

 字符串位置=41,0; 
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.Start();
var myPosition = watcher.Position;

if(!myPosition.Location.IsUnknown){
location = myPosition.Location.Latitude +,+ myPosition.Location.Longitude;

}

解决方案等待位置服务准备就绪。你的GeoCoordinateWatcher有一个状态改变事件和另一个位置改变事件。你的代码应该看起来像这样。

  //这可以在你的启动顺序中进行
_geoCoordinateWatcher.StatusChanged + =
新的EventHandler< GeoPositionStatusChangedEventArgs>(_ gcw_StatusChanged);
_geoCoordinateWatcher.PositionChanged + =
new EventHandler< GeoPositionChangedEventArgs< GeoCoordinate>>(_ gcw_PositionChanged);
_geoCoordinateWatcher.MovementThreshold = 50; //米
_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。位置;





$ b因为你似乎有些悲伤,所以我建议你先将一个消息框在状态改变事件中,你可以判断它是否在你的手机上触发,一旦你得到这个排序尝试位置改变等。



另外,你有没有试过去?您可能无法获得GPS锁定,并且手机信号塔位置无法正常工作。走出去,清除高楼。如果你住在高层,在阳台上或(最重要的)在屋顶上出去。


I've just tried my first WP7 application on a real device. My problem is that I use geolocation for one of the features, but I always get location unknown. I don't know if there's any way to grant location permissions to my app or if I am missing something. In the phone settings the location services are enabled, and maps app is working without any problem in finding my actual position. I've checked the GeoCoordinateWatcher.Permisson property and its value is "Granted". I already have this line <Capability Name="ID_CAP_LOCATION"/> in WMAppManifest.xml.

Any ideas to solve it?

[EDIT]

Here's my code. I've added the start line after you told me to do so, but I'm still having te problem.

 string location = "41,0";
 GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
 watcher.Start();
 var myPosition = watcher.Position;

 if (!myPosition.Location.IsUnknown) {
 location = myPosition.Location.Latitude + "," +  myPosition.Location.Longitude;

}

解决方案

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.

这篇关于位置始终返回“未知”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆