Reputation: 121
I'm trying to get location in my application using mvvmcross location plugin, but it's always throw null reference exception because CurrentLocation
and LastSeenLocation
are always null. GPS service is enabled on real android device and permisions has been configured in android manifest. I've tested app on genymotion, and if I turn on GPS it works. I can't understand, why location provider doesn't provide location on real device.
Here is code sample:
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;
}
}
Upvotes: 0
Views: 696
Reputation: 30883
The location information is available only after the OnLocation
callback is called. For example here is one way to get the location:
private void GetLocation()
{
IMvxLocationWatcher _locationWatcher = Mvx.Resolve<IMvxLocationWatcher>();
_locationWatcher.Start(new MvxLocationOptions() {Accuracy = MvxLocationAccuracy.Fine}, (location) => {
// Use location parameter to get location information},
OnError);
}
Upvotes: 0