// 対象となる緯度経度データ
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude.doubleValue, longitude.doubleValue);
// 緯度経度データから住所(文字列)を取得する
typeof(self) __weak wself = self;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError* error) {
if (error) {
NSLog(@"%@", error.description);
typeof(self) __weak wself = self;
dispatch_async(dispatch_get_main_queue(), ^{
[CommonClass showWithTitle:@""
message:@"アドレス情報が取得できませんでした。"
cancelButtonTitle:@"OK"
otherButtonTitles:nil
viewController:wself
tapBlock:nil];
});
return;
}
if (placemarks.count == 0) {
NSLog(@"No results were returned.");
typeof(self) __weak wself = self;
dispatch_async(dispatch_get_main_queue(), ^{
[CommonClass showWithTitle:@""
message:@"アドレス情報が取得できませんでした。"
cancelButtonTitle:@"OK"
otherButtonTitles:nil
viewController:wself
tapBlock:nil];
});
return;
}
for (CLPlacemark *placemark in placemarks) {
#if TRUE
NSString *addressString = @"";
NSDictionary *dict = placemark.addressDictionary;
NSArray *lines = dict[@"FormattedAddressLines"];
for (NSString *line in lines) {
addressString = [addressString stringByAppendingFormat:@"%@", line];
}
// 郵便番号があれば除去する
NSString *zip = [NSString stringWithObject:dict[@"ZIP"]];
addressString = [addressString stringByReplacingOccurrencesOfString:@"〒" withString:@""];
addressString = [addressString stringByReplacingOccurrencesOfString:zip withString:@""];
// 住所であるので、URLエンコードを忘れないこと!
addressString = [NSString urlEncodeWithStirng:addressString];
#else
// こちらの方法で住所を取得すると、一部重複して取得することがある
NSMutableString *mAddrStr = [NSMutableString new];
[mAddrStr appendFormat:@"%@", (placemark.administrativeArea) ? placemark.administrativeArea: @""];
[mAddrStr appendFormat:@"%@", (placemark.subAdministrativeArea) ? placemark.subAdministrativeArea: @""];
[mAddrStr appendFormat:@"%@", (placemark.locality) ? placemark.locality: @""];
//[str appendFormat:@"%@", (placemark.subLocality) ? placemark.subLocality: @""];
[mAddrStr appendFormat:@"%@", (placemark.thoroughfare) ? placemark.thoroughfare: @""];
[mAddrStr appendFormat:@"%@", (placemark.subThoroughfare) ? placemark.subThoroughfare: @""];
// 住所であるので、URLエンコードを忘れないこと!
NSString *addressString = [NSString urlEncodeWithStirng:mAddrStr];
#endif
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSString *coordiateString = [NSString stringWithFormat:@"%@,%@", latitude, longitude];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.co.jp/maps?"
@"f=q" // 左枠:通常(デフォルト)
@"&"
@"hl=ja" // 言語:日本
@"&"
@"z=20" // ズーム(0〜20)
@"&"
@"q=%@" // 地名 or 緯度経度(吹き出し表示位置)
@"&"
@"ll=%@" // 緯度経度(地図の中心地)
@"&"
@"iwloc=A" // 吹き出し表示(出せる場合だけ?)
,
addressString,
coordiateString];
wself.googleMapURL = urlString;
NSLog(@"urlString : %@", urlString);
// URLを使用しての何らかの連携処理
// 複数取得する場合があるが、先頭のデータを候補とする (どれが適切か判定できない為)
break;
}
}];
最終更新:2017年04月21日 09:20