CrossGeolocator's GetPositionAsync does not work

Try this:

Create a global variable:

private Position _position;

Then call ur method to get the position on constructor. Re-write ur method like this:

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

Then make a while where u want to use this:

while(_position == new Postion(0,0))
      GetPosition();

This worked for me.

Set up the xamarin forms maps as stated in the link. https://developer.xamarin.com/guides/xamarin-forms/user-interface/map/

set permissions as stated in below link https://jamesmontemagno.github.io/GeolocatorPlugin/GettingStarted.html

you may make use of https://jamesmontemagno.github.io/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's 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;

        }

    }
}

Tags:

C#

Mobile

Xamarin