Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

はじめに

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away