トップ «前の日記(2012-05-06) 最新 次の日記(2012-05-11)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2012-05-08 [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;
        }];
WayPoints

_ ソースコード

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冊には助けられた。

トップ «前の日記(2012-05-06) 最新 次の日記(2012-05-11)» 編集