Mvvmcross Location Plugin当前和上次看到的位置为空

时间:2016-04-29 07:50:01

标签: xamarin gps xamarin.android mvvmcross

我尝试使用mvvmcross位置插件在我的应用程序中获取位置,但它总是抛出空引用异常,因为CurrentLocationLastSeenLocation始终为空。在真实的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;
        }
    }

1 个答案:

答案 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);
}