Frage

I would like to get the user's current latitude and longitude for ios in xamarin.

I've looked at other posts and as i understand it.

it's like:

CLLocationManager lm = new CLLocationManager();
... (Acurray)
... (Other Specs)
lm.StartUpdatingLocation();
lm.Location.Coordinate.Latitude.ToString();
lm.Location.Coordinate.Longitude.ToString();

However, when i run it in the simulator it doesn't do anything. It doesn't ask me to turn on my location services or update my Label with the latitude and longitude.

Also, how do i make it check it location services are turned on and if they're not. How to make the user do it. (like the pop up on some apps asking you to turn on your gps)

War es hilfreich?

Lösung

CLLocationManger is async - it will fire events when the location is updated.

CLLocationManager lm = new CLLocationManager(); //changed the class name
... (Acurray)
... (Other Specs)

lm.LocationsUpdated += delegate(object sender, CLLocationsUpdatedEventArgs e) {
    foreach(CLLocation l in e.Locations) {
        Console.WriteLine(l.Coordinate.Latitude.ToString() + ", " +l.Coordinate.Longitude.ToString());
    }
};

lm.StartUpdatingLocation();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top