xcode
非同期処理のため、引き当てた住所をUIラベルで表示する...とかは、住所がセットされるタイミングに注意。
住所は、CLPlacemarkの中のmy_placemark.addressDictionaryの中のvalueForKey:@"FormattedAddressLines"を使用するが、郵便番号があったり、なかったり、頭に〒があったりなかったりと、でたらめなので、ある程度整形が必要。下のサンプルでは正規表現を使用。
住所は、端末(iPhone)の言語設定次第でローカライズされる。
CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray* placemarks, NSError* error){ NSString *my_str = [[NSString alloc] init]; if ([placemarks count] > 0){ CLPlacemark *my_placemark = [placemarks objectAtIndex:0]; NSArray *address = [my_placemark.addressDictionary valueForKey:@"FormattedAddressLines"]; my_str = @""; for(int i=0;i<[address count];i++){ my_str = [my_str stringByAppendingString:[address objectAtIndex:i]]; } //頭の郵便番号を除去 NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"^〒\\d{3}-\\d{4} (.+)" options:0 error:nil]; NSTextCheckingResult *match = [regexp firstMatchInString:my_str options:0 range:NSMakeRange(0, my_str.length)]; if(match.numberOfRanges > 0) //〒123-4567の場合 my_str = [my_str substringWithRange:[match rangeAtIndex:1]]; regexp = [NSRegularExpression regularExpressionWithPattern:@"^\\d{7} (.+)" options:0 error:nil]; match = [regexp firstMatchInString:my_str options:0 range:NSMakeRange(0, my_str.length)]; if(match.numberOfRanges > 0) my_str = [my_str substringWithRange:[match rangeAtIndex:1]]; //showLabel.text = my_str; NSLog(@"現在地---%@",my_str); location_name = my_str; }else{ my_str = @"住所の引当ができない"; } }];