はじめに
座標(緯度、経度)からその地点の国情報を知りたいときに、標準APIだけでできないか調べてみました。
いままでは現在位置から国を判別したいときに、Google Reverse Geocodingを使用していましたが、
有料化になったため、代替案がないかを知りたかったからです。
実装
CoreLocation FrameworkにCLGeocoderクラスに逆ジオコーディングできるメソッドがあります。
CLGeocoder *geocoding = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:35.169688 longitude:136.899713];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[geocoding reverseGeocodeLocation:location preferredLocale:locale completionHandler:^(NSArray< CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
CLPlacemark *placemark = placemarks.firstObject;
NSLog(@"country:%@", placemark.country);
NSLog(@"isoCountryCode:%@", placemark.ISOcountryCode);
}];
参考
reverseGeocodeLocation(_:completionHandler:) - CLGeocoder | Apple Developer Documentation
ios - Find city name and country from latitude and longitude in Swift - Stack Overflow