GPS 위치값을 사용해서 주소 문자열을 얻는 리버스 지오코딩(reverse geocoding)은
기존 iOS 4.x 에서는 다음과 같은 방식으로 구현 가능했다.

    // Reverse GeoCoding
    CLLocationCoordinate2D location2d = CLLocationCoordinate2DMake(bestEffortAtLocation.coordinate.latitude, bestEffortAtLocation.coordinate.longitude);
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc
                                   initWithCoordinate:location2d];
    geocoder.delegate = self; // MKReverseGeocoderDelegate 상속한 클래스를 지정한다.
    [geocoder start];//변환을 시작한다.

....

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    NSMutableDictionary *addrDic = [[NSMutableDictionary alloc] initWithDictionary:placemark.addressDictionary];
    NSLog(@"%@", ABCreateStringWithAddressDictionary(addrDic, NO) );
}



iOS 5.0 부터는 새로운 CLGeocoder 클래스를 사용해서 주소 문자열을 얻는 새로운 방식을 사용할 수 있다. 사용법의 간단한 예는 다음과 같다. 기본적으로 블럭을 사용하기때문에, Delegate 형태를 사용하는 것 보다 좀 더 손쉽다.


    CLGeocoder *rGeo = [[CLGeocoder alloc] init];
    [rGeo reverseGeocodeLocation:bestEffortAtLocation completionHandler:
     ^(NSArray *placemarks, NSError *err) {
         if( nil == placemarks ) return;
         CLPlacemark *place = [placemarks objectAtIndex:0];
         NSMutableDictionary *addrDic = [[NSMutableDictionary alloc] initWithDictionary:place.addressDictionary];
         [addrDic removeObjectForKey:@"Country"];
         [addrDic removeObjectForKey:@"CountryCode"];
         [addrDic removeObjectForKey:@"ZIP"];
         address = ABCreateStringWithAddressDictionary(addrDic, NO);
         NSLog(@"%@",place.addressDictionary);
}


결과로 얻은 주소 문자열은 딕셔러니 형태로 접근하면 큰 차이는 없어 보이는데, 실제로 새로운 API 의 결과값은 약간 이상한 점이 보이기도 한다. 예를 들면 Seoul 이라는 단어가 City 에서도, State 에서도 나타나기도 하는 등, 어딘가 모르게 아직 문제가 있어보인다.

편리하긴 한데, 미국 지역이 아니라면 좀 더 체크가 필요하다. iOS 5.0 미만의 OS 에서는 사용할 수 없음도 명심하자.



이 게시물을..