how to get the current gps coordinates in xamarin

六月ゝ 毕业季﹏ 提交于 2019-12-01 10:03:43

问题


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)


回答1:


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();


来源:https://stackoverflow.com/questions/25015791/how-to-get-the-current-gps-coordinates-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!