Forum Xamarin.Forms

GeoLocator returns null from second time

Hello Folks,

I have been trying get Location of the device using GeoLocator plugin. Everything works fine for the first time but it runs into NPE further. I have no idea whats happening here, Im new to Xamarin and trying to learn. I'm trying access only in iOS for now. Please help me, its been 3 days.

Here is the code.

public class FeedPage : ContentPage
{

    CLLocationManager locManager = new CLLocationManager();
    Position position;
    public FeedPage()
    {
        Title = "News Feed";
        Icon = "tab_feed.png";
        Padding = new Thickness(0, 20, 0, 0);

        var description = new Label
        {
            Text = "Learn how to use Xamarin to leverage your skills in Microsoft Visual Studio \" +\n\t\t\t\t\"and C# to build cross-platform apps that run on both Android and iOS, and embrace the features and capabilities of each platform."
        };

        var lattitude = new Label();

        var longitude = new Label();

        var stackLayout = new StackLayout();
        stackLayout.Children.Add(description);

        locManager.RequestWhenInUseAuthorization();

        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;


        async Task<double> getLocation(){
            var position =  await locator.GetLastKnownLocationAsync();
            return position.Latitude;
        }

        position.Latitude = getLocation().Result;
        stackLayout.Children.Add(lattitude);
        Content = stackLayout;


    }
}

I have added these add keys to info.plist and successfully gave access to location services.
NSLocationWhenInUseUsageDescription
This app needs to access location.
NSLocationAlwaysUsageDescription
This app needs to access location.

Thank you, happy holidays!

Santhosh

Sign In or Register to comment.