我正在使用此代码获得地标,但它没有给出城市名称。早些时候,我使用 MKReverse Geocoder 来获取获取城市名称的地标,但在 iOS 6 中它显示已弃用,因为 Apple 开发人员在 CLLocation 中添加了所有内容。
所以我使用了这段代码:
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *location = [locationManager location];
NSLog(@"location is %@",location);
CLGeocoder *fgeo = [[[CLGeocoder alloc] init] autorelease];
// Reverse Geocode a CLLocation to a CLPlacemark
[fgeo reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error){
// Make sure the geocoder did not produce an error
// before continuing
if(!error){
// Iterate through all of the placemarks returned
// and output them to the console
for(CLPlacemark *placemark in placemarks){
NSLog(@"%@",[placemark description]);
city1= [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
NSLog(@"city is %@",city1);
}
}
else{
// Our geocoder had an error, output a message
// to the console
NSLog(@"There was a reverse geocoding error\n%@",
[error localizedDescription]);
}
}
];
}
正如我在控制台中看到的那样, NSLog(@"%@",[placemark description]);
它给出的输出如下:- abc 道路名称、abc 道路名称、州名、国家/地区名称。