geocoder对象是用网络服务把经纬度坐标和用户熟悉的地标相互转换.地标是一个数据集合,包括街道,城市,州/省和国家等信息.
因为地geocoder是依赖于网络.
用CLGeocoder来获取地标信息
使用CLGeocoder
来开始反向地理编码的请求,创建这个类的实例并调用reverseGeocodeLocation:completionHandler:
方法.这个对象会开始异步的反向地理编码请求,并且把结果在completionHandler:
里返回.
@implementation MyGeocoderViewController (CustomGeocodingAdditions)
- (void)geocodeLocation:(CLLocation*)location forAnnotation:(MapLocation*)annotation
{
if (!geocoder)
geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:
^(NSArray* placemarks, NSError* error){
if ([placemarks count] > 0)
{
annotation.placemark = [placemarks objectAtIndex:0];
// Add a More Info button to the annotation's view.
MKPinAnnotationView* view = (MKPinAnnotationView*)[map viewForAnnotation:annotation];
if (view && (view.rightCalloutAccessoryView == nil))
{
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
}
}];
}
@end
将地名转换成坐标
[geocoder geocodeAddressString:@"1 Infinite Loop"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
}
}];