Subscribed unsubscribe Subscribe Subscribe

OngaLog

最近GOが楽しくて、

Swift CLLocationManagerを使って現在の住所を取得してみる

地図の緯度軽度はとれたので、現在の住所を取得した時の作業ログ。てっきりGoogleのGeo APIなんかをつかうのかと思っていましたが、Xcode上の機能だけで完結できる、難易度は高くない。

前回の続きから、 onga-tec.hatenadiary.jp

今回つくったのは、地図上のボタンを押すと現在地をAlertで表示する動作。英語で情報が帰ってきているがこの部分は端末の環境設定によるらしいがまだ日本語で試せていない。。

f:id:loosefingers:20160806185559p:plain

CLLocationManagerの情報を利用して以下のように取得する。

func getGeoLocation(manager: CLLocationManager){
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
            if error != nil {
                print(error!.localizedDescription)
                return
            }
            if placemarks!.count > 0 {
                let placemark = placemarks![0] as CLPlacemark
                let address = self.displayLocationInfo(placemark)
                let alert = UIAlertView()
                alert.title = "Address"
                alert.message = address
                alert.addButtonWithTitle("OK")
                alert.show()
            } else {
                print("error")
            }
        })
    }

Appleの本社の住所を取得した場合は以下のような情報が帰ってくる。

<GEOMapItemStorage: 0x7fd278f34030> {
    placeData =     {
        component =         (
                        {
                "cache_control" = UNCACHEABLE;
                status = "STATUS_FAILED";
                type = "COMPONENT_TYPE_TRANSIT_INFO";
            },
                        {
                "cache_control" = UNCACHEABLE;
                status = "STATUS_FAILED";
                type = "COMPONENT_TYPE_TRANSIT_SCHEDULE";
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_TRANSIT_ATTRIBUTION";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_TRANSIT_INCIDENT";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_HOURS";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_RATING";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_TEXT_BLOCK";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_FACTOID";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_FLYOVER";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_BOUNDS";
                value =                 (
                                        {
                        bounds =                         {
                            "map_region" =                             {
                                eastLng = "-122.0278321";
                                northLat = "37.3339991";
                                southLat = "37.3302951";
                                westLng = "-122.0347314";
                            };
                        };
                    }
                );
                "values_available" = 1;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_ROAD_ACCESS_INFO";
                value =                 (
                                        {
                        "access_info" =                         {
                            "road_access_point" =                             (
                                                                {
                                    drivingDirection = "ENTRY_EXIT";
                                    location =                                     {
                                        lat = "37.3315903";
                                        lng = "-122.0306826";
                                    };
                                }
                            );
                        };
                    }
                );
                "values_available" = 1;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_PLACE_INFO";
                value =                 (
                                        {
                        "place_info" =                         {
                            center =                             {
                                lat = "37.3318633";
                                lng = "-122.0295238";
                            };
                            timezone =                             {
                                identifier = "America/Los_Angeles";
                            };
                        };
                    }
                );
                "values_available" = 1;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_ENTITY";
                value =                 (
                                        {
                        entity =                         {
                            name =                             (
                                                                {
                                    locale = "en-US";
                                    "string_value" = "Apple Inc.";
                                }
                            );
                            type = AOI;
                        };
                    }
                );
                "values_available" = 1;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_ADDRESS";
                value =                 (
                                        {
                        address =                         {
                            "known_accuracy" = PARCEL;
                            "localized_address" =                             (
                                                                {
                                    address =                                     {
                                        formattedAddressLine =                                         (
                                            "Apple Inc.",
                                            "1 Infinite Loop",
                                            "Cupertino, CA  95014",
                                            "United States"
                                        );
                                        structuredAddress =                                         {
                                            administrativeArea = California;
                                            administrativeAreaCode = CA;
                                            areaOfInterest =                                             (
                                                "Apple Inc."
                                            );
                                            country = "United States";
                                            countryCode = US;
                                            fullThoroughfare = "1 Infinite Loop";
                                            geoId =                                             (
                                            );
                                            locality = Cupertino;
                                            postCode = 95014;
                                            subAdministrativeArea = "Santa Clara";
                                            subThoroughfare = 1;
                                            thoroughfare = "Infinite Loop";
                                        };
                                    };
                                    language = "en-US";
                                }
                            );
                        };
                    }
                );
                "values_available" = 1;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_AMENITIES";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_STYLE_ATTRIBUTES";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                ttl = 43200;
                type = "COMPONENT_TYPE_RESULT_SNIPPET";
                "values_available" = 0;
                version = 10;
                "version_domain" =                 (
                    apple,
                    revgeo,
                    US
                );
            }
        );
        referenceFrame = WGS84;
        "result_provider_id" = 7618;
        status = "STATUS_SUCCESS";
    };
}

今回作成したサンプル。 gist.github.com

気になったのは住所を取得した時、たまに以下のエラーが発生する。連続で動作させた場合に発生率が高まるような気がします。

Reverse geocoder failed with errorThe operation couldn’t be completed. (kCLErrorDomain error 2.)

ここにもそのような事が書いてある。連続でのリクエストでエラーが発生するのはApple側の仕様なのだろうか、? stackoverflow.com