[iOS]位置情報と住所 | Cocoa練習帳

[iOS]位置情報と住所

位置情報から住所を取得するのに挑戦だ。


iOS 5からMKReverseGeoCoderの利用は推奨されず、かわりに、CLGeocoderの利用が推奨されるようになった。CLGeocoderはBlocksを使ったモダンなクラスなので、この変更は歓迎すべきではなるのだが、問題は、情報が少ない。リファレンスの説明もよく分からない。そこで、試行錯誤しながら、進めてゆく。




以前の位置情報を取得するメソッドで、住所を取得するコードを追加する。




    self.geocoder = [[CLGeocoder alloc] init];
...
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];
    
    GPXTrackPoint   *trkpt = nil;
    trkpt = [self.document.gpxTrack newTrackpointWithLatitude:newLocation.coordinate.latitude
                                                    longitude:newLocation.coordinate.longitude];
    trkpt.time = newLocation.timestamp;
    
    NSString    *s = [[NSString alloc] initWithFormat:@"(%f, %f)",
                      newLocation.coordinate.latitude,
                      newLocation.coordinate.longitude];
    self.messageLabel.text = s;
    
    [self.geocoder reverseGeocodeLocation:newLocation completionHandler:
        ^(NSArray* placemarks, NSError* error) {
            住所を取得
        }];
}



ここで問題なのは、ブロックの引数のplacemarksの内容がよく分からない。placemarksはCLPlacemarkの配列という情報を得て、CLPlacemarkにはaddressDictionaryというプロパティがあるので、それをダンプしてみた。




[self.geocoder reverseGeocodeLocation:newLocation completionHandler:
        ^(NSArray* placemarks, NSError* error) {
            if(!error){
                for(CLPlacemark *placemark in placemarks){
                    for (NSString *key in placemark.addressDictionary.allKeys) {
                        NSLog(@"Key: %@", key);
                    }
                }
            }
        }];



以下がその結果だ。




2012-05-08 20:54:44.188 WayPoints[7456:f803] Key: FormattedAddressLines
2012-05-08 20:54:44.189 WayPoints[7456:f803] Key: Street
2012-05-08 20:54:44.190 WayPoints[7456:f803] Key: SubAdministrativeArea
2012-05-08 20:54:44.191 WayPoints[7456:f803] Key: Thoroughfare
2012-05-08 20:54:44.192 WayPoints[7456:f803] Key: ZIP
2012-05-08 20:54:44.193 WayPoints[7456:f803] Key: Name
2012-05-08 20:54:44.195 WayPoints[7456:f803] Key: City
2012-05-08 20:54:44.196 WayPoints[7456:f803] Key: PostCodeExtension
2012-05-08 20:54:44.197 WayPoints[7456:f803] Key: Country
2012-05-08 20:54:44.198 WayPoints[7456:f803] Key: State
2012-05-08 20:54:44.198 WayPoints[7456:f803] Key: SubLocality
2012-05-08 20:54:44.199 WayPoints[7456:f803] Key: SubThoroughfare
2012-05-08 20:54:44.201 WayPoints[7456:f803] Key: CountryCode



このキーのFormattedAddressLinesで指されるのが、NSStringの配列のようで、これをダンプしてみた。




[self.geocoder reverseGeocodeLocation:newLocation completionHandler:        ^(NSArray* placemarks, NSError* error) {
            NSMutableString *str = [NSMutableString stringWithString:@""];
            if (!error) {
                for (CLPlacemark *placemark in placemarks) {
                    for (NSString *key in placemark.addressDictionary.allKeys) {
                        NSLog(@"Key: %@", key);
                    }
                    NSArray *array = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
                    for (NSString *line in array) {
                        [str appendString:line];
                        [str appendString:@", "];
                    }
                    [str appendString:@"\n"];
                }
            }
            else {
                str = [NSString stringWithFormat:@"error: %@", error];
            }
            NSLog(@"%@", str);
            self.gpxTextView.text = str;
        }];

run


ソースコード

GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/ios/WayPoints - GitHub




関連情報

iOS GPX Framework

GitHub

This is a iOS framework for parsing/generating GPX files. This Framework parses the GPX from a URL or Strings and create Objective-C Instances of GPX structure.

iOSプログラミング逆引きリファレンス108 ~知りたいことがすぐわかるiPhoneプログラミングテクニック~

iPhoneアプリ開発 熟達テクニック

上記、2冊には助けられた。