我尝试使用mvvmcross位置插件在我的应用程序中获取位置,但它总是抛出空引用异常,因为CurrentLocation
和LastSeenLocation
始终为空。在真实的Android设备上启用了GPS服务,并且在Android清单中配置了permisions。我已经在genymotion上测试了应用程序,如果我打开GPS就行了。我无法理解,为什么位置提供商不在真实设备上提供位置。
这是代码示例:
private void GetLocation()
{
IMvxLocationWatcher _locationWatcher = Mvx.Resolve<IMvxLocationWatcher>();
_locationWatcher.Start(new MvxLocationOptions() {Accuracy = MvxLocationAccuracy.Fine}, OnLocation, OnError );
try
{
Lat = _locationWatcher.CurrentLocation.Coordinates.Latitude;
Lng = _locationWatcher.CurrentLocation.Coordinates.Longitude;
}
catch
{
Lat = _locationWatcher.LastSeenLocation.Coordinates.Longitude;
Lng = _locationWatcher.LastSeenLocation.Coordinates.Longitude;
}
}
答案 0 :(得分:0)
只有在调用OnLocation
回调后,位置信息才可用。例如,这是获取位置的一种方法:
private void GetLocation()
{
IMvxLocationWatcher _locationWatcher = Mvx.Resolve<IMvxLocationWatcher>();
_locationWatcher.Start(new MvxLocationOptions() {Accuracy = MvxLocationAccuracy.Fine}, (location) => {
// Use location parameter to get location information},
OnError);
}