Why can't I get my current location?

I need to be able to capture the current device gps location as soon as the App starts. Here is the code I'm using.

location = new CLLocationManager ();

location.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
Console.WriteLine ("Location Updated: {0}", e.Locations[e.Locations.Length - 1]);
};

location.StartUpdatingLocation();

Despite following all the guides I'm getting noting. The location updated event never seems to get called, there is no message to enable location services for the app. Nothing ever happens. It just sits there doing nothing. What is going on here? Why is it so difficult just to get your basic location coordinates?

Best Answer

  • rmaciasrmacias US ✭✭✭✭✭
    edited September 2014 Accepted Answer

    If you're compiling for iOS 8, then make sure you call the RequestWhenInUseAuthorization() method and add the NSLocationWhenInUseUsageDescription key in you info.plist file. Both of these are necessary in order for CLLocationManager to work in iOS 8. That's assuming you're running in iOS 8.

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Perhaps you are doing that work too early. When are you doing it?

  • AustinHiceAustinHice USMember

    The initial starting screen. The initial view controller contains a view with a basic loading screen during which it is intended to collect important data such as the user's current location. This code is in the controller's ViewDidLoad.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    ViewDidLoad is probably being called from within FinishedLaunching (or earlier), which I suspect is too early. Try moving it to ViewDidAppear.

  • AustinHiceAustinHice USMember

    Moving the code had no effect. I'm still getting nothing.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Maybe you already got the prompt once and said no. Try deleting the app and reinstalling it (or find the setting and explicitly give permissions, or reset the warnings as described here: http://stackoverflow.com/questions/4187803/cllocationmanager-not-prompting-the-user-and-returns-null).

  • AustinHiceAustinHice USMember

    Definitely not the case. It seems only manually changing the permissions in the settings will work, but that's not really a solution. I need to ensure these settings can be set in the app itself, as is the expected behavior in all apps.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Did you try deleting the app and using ViewDidAppear? I'm still convinced ViewDidLoad is too early ,and your app may have gotten into a bad state by calling it then.

    If none of this helps I suggest you google it or search Stack Overflow. I'm pretty sure this isn't a Xamarin-specific issue, and you will have better luck searching within the broader iOS community.

  • OlofDrevinOlofDrevin USMember

    Hi I have the same problem. After a days research I found out that my app simply didn't ask for access to the gps. I have to manually change the setting (Settings -> Privacy -> Location) every time i rebuild the application, for it to get the LocationsUpdated events. And I'm quite sure I never ever saw my app ask for gps permission.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited September 2014 Accepted Answer

    If you're compiling for iOS 8, then make sure you call the RequestWhenInUseAuthorization() method and add the NSLocationWhenInUseUsageDescription key in you info.plist file. Both of these are necessary in order for CLLocationManager to work in iOS 8. That's assuming you're running in iOS 8.

  • ThomasDepoleThomasDepole USMember ✭✭

    Hey guys, I'm having the same weird issue. The demo app runs fine, but my app with the same LocationManager class with the proper permissions never asks for location permissions. It only works once when I goto the settings > privacy > location settings and select Always.

    One other weird thing I noticed is that when I goto privacy settings it just has the option for "Never" and it's not selected. Once I select "Never" go back and click on it again it has the other three options.

    Were any of you able to figure out what's causing that weird behavior?

  • ThomasDepoleThomasDepole USMember ✭✭

    I was able to figure out what I was missing. My background tracking started working properly after adding this to the info.plish. Hope this helps anyone else that runs into this issue!

Sign In or Register to comment.