Hi,
I'm using Xamarin.Mobile to get the curernt location for the device. I have read the it does not work in the simulator.
Now I have testet it on a device (IOS 9.1)
But with no luck.
The GetPositionAsync never returns.
`public async Task GetCurrentLocation()
{
var locator = new Geolocator { DesiredAccuracy = 50 };
var cancelSource = new CancellationTokenSource();
var location = GeoLocation.Undefined;
await locator.GetPositionAsync(1000, cancelSource.Token).ContinueWith( t => location = t.IsCompleted ? new GeoLocation(t.Result.Latitude, t.Result.Longitude) : GeoLocation.Undefined, cancelSource.Token); return location; }`
Anyone that knows what to do?
Br Morten
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeout: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
IMPORTANT
Android:
You must request ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION permission
Android 6.0 Marshmallow You will have to ask for Fine&Course Location permissions via the new runtime permissions. https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/
See this example: https://github.com/jamesmontemagno/MarshmallowSamples/blob/master/RuntimePermissions/MarshmallowPermission/MainActivity.cs
iOS:
In iOS 8 you now have to call either RequestWhenInUseAuthorization or RequestAlwaysAuthorization on the location manager (the plugin does this automatically for you, however, need to add either the concisely named NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription to your Info.plist.
You will need to add a new string entry called NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription.
Go to your info.plist and under source add one of these flags: http://screencast.com/t/YEeuAYMBBJ
For more information: http://motzcod.es/post/97662738237/scanning-for-ibeacons-in-ios-8
iOS 9 Simulator Getting location via the simulator doesn't seem to be supported, you will need to test on a device.
iOS 9 Special Case: Background Updates (for background agents, not background tasks):
New in iOS 9 allowsBackgroundLocationUpdates must be set if you are running a background agent to track location. I have exposed this on the Geolocator via:
var locator = CrossGeolocator.Current; locator.AllowsBackgroundUpdates = true;
The presence of the UIBackgroundModes key with the location value is required for background updates; you use this property to enable and disable the behavior based on your app’s behavior.
Windows Phone:
You must set the ID_CAP_LOCATION permission.
Got it working.
I was using a wrong package.
When I downloaded the nuget package everything start working perfectley.
Xam.Plugin.Geolocator https://www.nuget.org/packages/Xam.Plugin.Geolocator
Thanks @AlessandroCaliaro
Answers
Hello all!
Similar problem here
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeout: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
IMPORTANT
Android:
You must request ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION permission
Android 6.0 Marshmallow You will have to ask for Fine&Course Location permissions via the new runtime permissions. https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/
See this example: https://github.com/jamesmontemagno/MarshmallowSamples/blob/master/RuntimePermissions/MarshmallowPermission/MainActivity.cs
iOS:
In iOS 8 you now have to call either RequestWhenInUseAuthorization or RequestAlwaysAuthorization on the location manager (the plugin does this automatically for you, however, need to add either the concisely named NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription to your Info.plist.
You will need to add a new string entry called NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription.
Go to your info.plist and under source add one of these flags: http://screencast.com/t/YEeuAYMBBJ
For more information: http://motzcod.es/post/97662738237/scanning-for-ibeacons-in-ios-8
iOS 9 Simulator Getting location via the simulator doesn't seem to be supported, you will need to test on a device.
iOS 9 Special Case: Background Updates (for background agents, not background tasks):
New in iOS 9 allowsBackgroundLocationUpdates must be set if you are running a background agent to track location. I have exposed this on the Geolocator via:
var locator = CrossGeolocator.Current; locator.AllowsBackgroundUpdates = true;
The presence of the UIBackgroundModes key with the location value is required for background updates; you use this property to enable and disable the behavior based on your app’s behavior.
Windows Phone:
You must set the ID_CAP_LOCATION permission.
Yes, I have already tried this. But with no luck.
var position = await locator.GetPositionAsync(10000);
never returns
Got it working.
I was using a wrong package.
When I downloaded the nuget package everything start working perfectley.
Xam.Plugin.Geolocator https://www.nuget.org/packages/Xam.Plugin.Geolocator
Thanks @AlessandroCaliaro
the same problem, how do you solve it Morten?
when i call from my pcl project var position = await locator.GetPositionAsync(10000); it never returns...
Hey, same problem here.
I'm sending a silent remote notification to wake up the device, get his location using locator.GetPositionAsync but this method never returns.
All the config like UIBackgroundModes key, allowsBackgroundLocationUpdates and NSLocationAlwaysUsageDescription is OK.
Anyone on this?
Thank you
Has anyone ever got a solution for this ?
Im in android and pushed it out to a class... but never returns
No errors etc....
public async Task GetLocationAsync() ..... var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 1000; if (locator.IsGeolocationEnabled){ position = await locator.GetPositionAsync(20000); }
called from separate class
using(DeviceLocation deviceLocation = new DeviceLocation(globalContext)) { await deviceLocation.GetLocationAsync(); current_Lat = deviceLocation.current_Lat(); current_Lng = deviceLocation.current_Lng(); }
Now the funny thing is
If i put code on my main cs as per example code on git (link above in other comment) it works no problems....
Its as if its not awaiting or something else is going on ..
Using GeoLocator Plugin Version 3.0.4 (Stable) and 4.0.* Beta targeting Android 7.1.
Its unable to fetch location using Stable (3.0.4) Plugin Version. But with beta version of Plugin its giving "Could not resolve type with token 010001fe" xamarin.geolocator. But Plugin is working fine for Lower target versions.
try this
var position = await locator.GetPositionAsync(timeout: new TimeSpan(10000));
This works like a charm -
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10000), null, true);
Thanks Nitesh
For the latest stable verison 4.1.3 this work like you said.
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10000), null, true);
I had this working then tried to make some unit tests to test my class that uses Geolocator. It is in an android test runner with the proper nuget packages and this just refuses to work. It didn't work originally in my project but something fixed it.
Why can't I just get the position?
In case no-one can get it fixed with the fix above please try to confirm that the location mode on the android device is set to high Accuracy.
I had it in "Device Only" and the GetLocationAsynch was always timing out.
On Lenovo Android device go to Settings -> Security&Location -> Location -> Mode and set it to "High Accuracy"
You could implement your servince in ios and get last position from it.