Windows phone 8 GetGeopositionAsync不返回
在我的Windows Phone 8应用程序中,我试图使用主页上的GetGeopositionAsync根据用户位置显示一些项目 调用GetGeopositionAsync不会在指定的超时内返回,它根本不会返回 我使用的代码很简单:Windows phone 8 GetGeopositionAsync不返回,windows-phone-8,Windows Phone 8,在我的Windows Phone 8应用程序中,我试图使用主页上的GetGeopositionAsync根据用户位置显示一些项目 调用GetGeopositionAsync不会在指定的超时内返回,它根本不会返回 我使用的代码很简单: Geolocator geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 50; Geoposition g
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
Geoposition geoposition = null;
try
{
geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
}
catch (UnauthorizedAccessException ex)
{
// location services disabled or other error
// user should setup his location
}
我能找到的解决方案是为GeoCoordinateWatcher创建一个异步包装器,它似乎工作得很好。
但是我对我的解决方案不是很有信心,我更喜欢使用GetGeopositionAsync,它看起来像是WP8中获取设备位置的推荐方法
更新:其他人报告了相同的行为:
这很奇怪,但GetGeoPositionSync仅在使用MovementThreshold和/或ReportInterval初始化Geolocator时返回当前位置
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
geolocator.MovementThreshold = 5;
geolocator.ReportInterval = 500;
Geoposition geoposition = null;
try
{
geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
}
catch (UnauthorizedAccessException ex)
{
// location services disabled or other error
// user should setup his location
}
您何时调用该方法来请求地理位置?我发现在ViewModel中调用构造函数时遇到了同样的问题
我可以通过添加一个onload命令并从那里调用该方法来修复代码中的问题。从那以后,我就再也没有问题了。我在设备上测试时遇到了这个问题。我必须禁用设备上的WiFi才能让它工作。我知道有些人在使用模拟器时遇到了相反的问题。我不必做任何包装。希望能有所帮助我也遇到了上述一些问题。当我连接到geolocator.StatusChanged事件时,我注意到事件的顺序是:
while (geolocator.LocationStatus == PositionStatus.Initializing)
{
System.Threading.Thread.Sleep(100);
}
这是不雅观的,但确实有效。当调用
GetGeoPositionSync()
时,geolocator
的状态处于NotInitialized
状态时,就会发生这种奇怪的行为
地理定位器
仅在两种情况下可用。第一,当订阅PositionChanged
事件时。第二,当已经调用了GetGeopositionAsync()
时
因此,在调用GetGeopositionAsync()
之前,您只需将geological
订阅到positionChanged
事件
希望这有帮助。我知道这有点老,但我希望其他搜索此主题的人会发现此答案有帮助 在尝试访问定位服务之前,请确保获得用户的同意 我遇到了这个问题,但通过在打开页面以获得他们的同意时调用OnNavigatedTo事件来修复它
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
// User has opted in or out of Location
return;
}
else
{
MessageBoxResult result =
MessageBox.Show("This app accesses your phone's location. Is that ok?",
"Location",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
}else
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
}
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
我发现了一件事。若我将精度设置为更大的精度,那个么地理定位器将开始返回坐标。所以它在50米范围内不起作用,但在500米范围内起作用,所以试着用下面的线代替
geolocator.DesiredAccuracyInMeters = 500;
我发现,如果您在本地创建了地理定位器,该任务最终将被取消。当我创建一个永久地理定位器实例时,它就可以工作。请参阅我的示例: 它使用MVVM和Cimbalino工具包
在我的例子中,我设置了ReportInterval=5来解决这个问题。看起来每个人都被黑客攻击了,直到它成功。。。以下是对我有效的方法:
/// <summary>
/// HACK: For some reason Geolocator.GetGeopositionAsync hangs indefinitely.
/// The workaround is to add a PositionChanged handler.
/// </summary>
private Geoposition GetGeoposition()
{
var geolocator = new Geolocator();
var semaphoreHeldUntilPositionReady = new SemaphoreSlim(initialCount: 0);
Geoposition position = null;
geolocator.ReportInterval = 1000;
geolocator.PositionChanged += (sender, args) =>
{
position = args.Position;
semaphoreHeldUntilPositionReady.Release();
};
semaphoreHeldUntilPositionReady.Wait();
return position;
}
//
///黑客:由于某些原因,geologitor.GetGeopositionAsync无限期挂起。
///解决方法是添加一个位置更改处理程序。
///
私有地理位置GetGeoposition()
{
var geologitor=新的geologitor();
var semaphoreHeldUntilPositionReady=新信号量lim(初始计数:0);
地理位置=空;
geolocator.ReportInterval=1000;
geolocator.PositionChanged+=(发送方,参数)=>
{
位置=参数位置;
semaphoreHeldunilPositionReady.Release();
};
semaphoreHeldunilPositionReady.Wait();
返回位置;
}
名为“IsBusy”的变量始终是创建死锁的好方法。我们看不到您在其他地方使用它。请忽略这一点。我在Wait之后和catch子句中放置了断点,两个断点都未到达。您是否调用调用堆栈上的Wait
或Result
?这正是我提出的解决方案,它完全解决了我的问题。从page调用。Loaded event也为我解决了问题。在我以前打电话给虚拟机构造器之前。向下投票,因为你的答案与OP无关。你的应用程序将无法通过认证,但同意与地理位置/地理位置工作与否无关。这就是解决方案!!为了得到它,我只设置了这两个地理定位器。DesiredAccuracyInMeters=2;geolocator.MovementThreshold=3;谢谢你的提示!