Xamarin.ios CLLocationManager和CLGeoCoder
我想使用实际位置的坐标(CLLocationManager)来反向地理编码(CLGeoCoder)。 我有以下代码:Xamarin.ios CLLocationManager和CLGeoCoder,xamarin.ios,cllocationmanager,clgeocoder,Xamarin.ios,Cllocationmanager,Clgeocoder,我想使用实际位置的坐标(CLLocationManager)来反向地理编码(CLGeoCoder)。 我有以下代码: locationMgr = new CLLocationManager(); locationMgr.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters; locationMgr.DistanceFilter = 10; locationMgr.UpdatedL
locationMgr = new CLLocationManager();
locationMgr.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters;
locationMgr.DistanceFilter = 10;
locationMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) => {
Task.latitude = e.NewLocation.Coordinate.Latitude;
Task.longitude = e.NewLocation.Coordinate.Longitude;
locationMgr.StopUpdatingLocation();
};
btnLocation = new UIBarButtonItem(UIImage.FromFile("Icons/no-gps.png"), UIBarButtonItemStyle.Plain, (s,e) => {
if (CLLocationManager.LocationServicesEnabled) {
locationMgr.StartUpdatingLocation();
geoCoder = new CLGeocoder();
geoCoder.ReverseGeocodeLocation(new CLLocation(Task.latitude, Task.longitude), (CLPlacemark[] place, NSError error) => {
adr = place[0].Name+"\n"+place[0].Locality+"\n"+place[0].Country;
Utils.ShowAlert(XmlParse.LocalText("Poloha"), Task.latitude.ToString()+"\n"+Task.longitude.ToString()+"\n\n"+adr);
});
}
else {
Utils.ShowAlert(XmlParse.LocalText("PolohVypnut"));
}
});
因为UpdatedLocation()需要几秒钟,所以ReverseGeocodeLocation()的输入是Task.latitude=0和Task.longitude=0
在ReverseGeoDelocation()之前,如何等待正确的值(Task.latitude、Task.longitude)
感谢您的帮助。在
CLLocationManager
获取位置之前,将调用地理编码器的反向地理位置
方法
调用StartUpdatingLocation
并不意味着将立即触发UpdatedLocation
事件。此外,如果您在iOS 6上,UpdatedLocation
将永远不会被触发。改用LocationsUpdated
事件
例如:
locationManager.LocationsUpdated += (sender, args) => {
// Last item in the array is the latest location
CLLocation latestLocation = args.Locations[args.Locations.Length - 1];
geoCoder = new CLGeocoder();
geoCoder.ReverseGeocodeLocation(latestLocation, (pl, er) => {
// Read placemarks here
});
};
locationManager.StartUpdatingLocation();
在启动反向地理代码查找之前,请等待UpdatedLocation事件触发。