简体   繁体   中英

Get user's current location in Xamarin iOS

I would like to get the user's current location in iOS Xamarin. Here is my code.

CLLocationManager locationManager = new CLLocationManager();
locationManager.StartUpdatingLocation();
locationManager.StartUpdatingHeading();

locationManager.LocationsUpdated += delegate(object sender , CLLocationsUpdatedEventArgs e )
{
    foreach(CLLocation loc in e.Locations)
    {
        Console.WriteLine(loc.Coordinate.Latitude);
    }
};

1.When i launch my app, a dialog "AppName would like to use your current location" pops in and after one or two seconds it dismisses by itself(before i could click any button). Why is this happening?

2. App works fine when i manually(settings->privacy->locations) enable location services for my app. I want this to happen programmatically. I have checked this post also similar question .Where am i going wrong?

Please help

Is this for iOS8? Did you call RequestWhenInUseAuthorization?

As for the dismiss of the dialog, that is most likely due to the locationManager variable not being declared at the class level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM