문제

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)

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top