LoginSignup
1
2

More than 5 years have passed since last update.

座標から国コードを取得する方法

Posted at

はじめに

座標(緯度、経度)からその地点の国情報を知りたいときに、標準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

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2