CrossGeolocator GetPositionAsync not working

I use CrossGeolocator to get the current latitude and longitude of the device. However, I use it inside the OnAppearing method and it does not work. The GetPositionAsync method hangs in the application.

protected override void OnAppearing() { base.OnAppearing(); var position = GetPosition().Result; var lat = position.Latitude; var lon = position.Longitude; } private static async Task<Position> GetPosition() { var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 50; var position = await locator.GetPositionAsync(10000); return position; } 

In particular, I use this GetPosition method in application buttons and works fine.

Can someone help me on this?

+8
c # mobile xamarin
source share
5 answers

Try the following:

Create a global variable:

 private Position _position; 

Then call the ur method to get the position in the constructor. Overwrite the ur method as follows:

 public async void GetPosition() { var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 50; var myPosition = await locator.GetPositionAsync(); _position = new Position(myPosition.Latitude, myPosition.Longitude); } 

Then do some time when you want to use this:

 while(_position == new Postion(0,0)) GetPosition(); 
+4
source share

It worked for me.

Configure xamarin form maps as indicated in the link. https://developer.xamarin.com/guides/xamarin-forms/user-interface/map/

set the permissions indicated in the link below https://jamesmontemagno.imtqy.com/GeolocatorPlugin/GettingStarted.html

you can use https://jamesmontemagno.imtqy.com/GeolocatorPlugin/CurrentLocation.html

 using Plugin.Geolocator; using System; using Xamarin.Forms; using Xamarin.Forms.Maps; using Xamarin.Forms.Xaml; namespace MapApp { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MapPage : ContentPage { private Position _position; public MapPage() { InitializeComponent(); var map = new Map( MapSpan.FromCenterAndRadius( new Position(37, -122), Distance.FromMiles(0.3))) { IsShowingUser = true, HeightRequest = 100, WidthRequest = 960, VerticalOptions = LayoutOptions.FillAndExpand }; if (IsLocationAvailable()) { GetPosition(); map.MoveToRegion(MapSpan.FromCenterAndRadius(_position, Distance.FromMiles(1))); } map.MapType = MapType.Street; var stack = new StackLayout { Spacing = 0 }; stack.Children.Add(map); Content = stack; } public bool IsLocationAvailable() { if (!CrossGeolocator.IsSupported) return false; return CrossGeolocator.Current.IsGeolocationAvailable; } public async void GetPosition() { Plugin.Geolocator.Abstractions.Position position = null; try { var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 100; position = await locator.GetLastKnownLocationAsync(); if (position != null) { _position = new Position(position.Latitude, position.Longitude); //got a cahched position, so let use it. return; } if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled) { //not available or enabled return; } position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true); } catch (Exception ex) { throw ex; //Display error as we have timed out or can't get location. } _position = new Position(position.Latitude, position.Longitude); if (position == null) return; } } } 
+2
source share

Things to keep in mind

method call in the constructor. have lat lat available if it exists. VERY IMP tried to catch without trying to catch application attacks. Do not write anything in the trick, because you do not want to break the exception.

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using Foundation; using UIKit; using System.Threading.Tasks; using Plugin.Geolocator.Abstractions; using Plugin.Geolocator; using System.Diagnostics; using Xamarin.Forms; using abcde.iOS; [assembly: Xamarin.Forms.Dependency(typeof(GPSLocation_iOS))] namespace abcde.iOS { public class GPSLocation_iOS : IGPSLocation { public Position _position; public GPSLocation_iOS() { GetPosition(); } public Dictionary<string, string> GetGPSLocation() { Dictionary<string, string> dictPosition = new Dictionary<string, string>(); dictPosition.Add("latitude", _position.Latitude.ToString()); dictPosition.Add("longitude", _position.Longitude.ToString()); return dictPosition; } public async void GetPosition() { try { var locator = CrossGeolocator.Current; _position = await locator.GetLastKnownLocationAsync(); if (_position == null) { locator.DesiredAccuracy = 50; var myPosition = await locator.GetPositionAsync(); _position = new Position(myPosition.Latitude, myPosition.Longitude); } } catch (Exception ex) { } } 
+1
source share

latest update version 2018-07-10,

To support iOS 11 and earlier, you can enable all three keys:

  • NSLocationWhenInUseUsageDescription,
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription.

I use Xamarin to write an application and it works fine in my iOS project

link: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/map

0
source share

I solved my problem by simply adding parameters to GetPositionAsync: null and true.

var location = await locator.GetPositionAsync (TimeSpan.FromSeconds (20), null, true);

and turn on GPS

My problem was on Android 4.4

0
source share

All Articles