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.
}
}];