在我的Windows Phone 8应用程序中,我尝试在主页面上使用GetGeopositionAsync来显示基于用户位置的一些项目.
调用GetGeopositionAsync不会在指定的超时内返回,它根本不返回.
我使用的代码很简单:
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
}
Run Code Online (Sandbox Code Playgroud)
我能找到的解决方案是为GeoCoordinateWatcher创建一个异步包装器,它似乎工作正常.但我对我的解决方案不太自信,我更愿意使用GetGeopositionAsync,它看起来像是在WP8中获取设备位置的推荐方式.
更新:其他人报告相同的行为:http: //social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/ff166fac-b423-4428-abd8-610bf0102fc0
小智 5
您何时调用方法来请求地理位置?当我在ViewModel中调用构造函数的一部分时,我发现遇到了同样的问题.
通过添加OnLoadedCommand并从那里调用方法,我能够在代码中解决问题.从那以后我就没有进一步的问题了.
这很奇怪,但 GetGeoPositionAsync 仅在使用 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6289 次 |
| 最近记录: |