使用 CLLocation 对象(内容例如纬度 = 48.196169,经度 = 11.620237),我尝试获取当前城市和国家/地区,例如:
if(!geocoder) {
geocoder = [[CLGeocoder alloc] init];
}
if (geocoder.geocoding) [geocoder cancelGeocode];
[geocoder reverseGeocodeLocation:lo completionHandler:^(NSArray *placemarks, NSError *error) {
if(devMode) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
}
if (error == nil && [placemarks count] > 0) {
// MY CODE - here placemarks is always (null)
} else {
if(devMode)
NSLog(@"%@", error.debugDescription);
}
}];
大多数情况下,效果很好。但在极少数情况下,我只会收到错误:
PBRequester 失败,错误错误域 = NSURLErrorDomain 代码 = -1000
“Ungültige URL”UserInfo = 0x16f5ff30 {NSUnderlyingError = 0x16f57810
“Ungültige URL”,NSLocalizedDescription=Ungültige URL}
找到地标:(空),错误:错误域=kCLErrorDomain代码=2
“操作无法完成。(kCLErrorDomain 错误 2。)”
我完全不知道为什么会发生这种情况。
您可以使用以下代码来查找地址行,这里您必须传递纬度和经度作为参数
- (void)findAddress:(CLLocationDegrees)latitude with:(CLLocationDegrees)longitude{
CLLocation *location =[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Finding address");
if (error) {
NSLog(@"Error %@", error.description);
} else {
NSLog(@"%@",placemarks[0]);
}
}]; }
在这里您必须导入以下内容#import <CoreLocation/CLGeocoder.h>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)