1
I need to get the current location of the user’s device and with this to help me I am using the Geolocator Plugin of Xamarin, I can get the current location normally on my smartphone but the Google emulator doesn’t pick up the current location even with the GPS set up for the city of Rio de Janeiro, and the strange thing is that it always points to Rome.
The Android location permissions are enabled and everything configured correctly. For this I used as a basis this project https://github.com/jamesmontemagno/GeolocatorPlugin.
Follow the code on my page:
Builder:
Public MapsPage()
{
InitializeComponent();
var position = PosicaoAtualAsync();
map.MoveToRegion(MapSpan.FromCenterAndRadius(position.Result, Distance.FromMeters(50)));
}
Posicaoatualasync method:
async Task<Position> PosicaoAtualAsync()
{
var locator = CrossGeolocator.Current;
if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable)
{
locator.DesiredAccuracy = 100;
if (!locator.IsListening)
await locator.StartListeningAsync(1000, 1000);
var position = await locator.GetPositionAsync(timeoutMilliseconds: 1000);
if (position != null)
{
return new Position(position.Latitude, position.Longitude);
}
}
await DisplayAlert("Ops", "Não conseguimos obter sua localização, por favor, verifique sua conexão e se o seu GPS está ativo.", "OK");
return new Position(0, 0);
}