GeoCoder 使用地址获取经度和纬度

GeoCoder using address to get longitude and latitude

我正在尝试使用地理编码器从地址中提取纬度和经度。不幸的是,当我 运行 下面的代码时,我得到了一个非常长的苹果商店所有位置的信息列表。下面是我用来打印展示位置的代码。

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
            if let placemark = placemarks?[0] as? CLPlacemark {
               println(placemark)
            }
            })

我只想要纬度和经度,这样我就可以保存它。输出结果为:

Infinite Loop<GEOMapItemStorage: 0x7fe4aaf3e4f0> {
    placeData =     {
        component =         (
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_HOURS";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_RATING";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_FLYOVER";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_BOUNDS";
                value =                 (
                                        {
                        bounds =                         {
                            "map_region" =                             {
                                eastLng = "-122.029843";
                                northLat = "37.3321671";
                                southLat = "37.3312164";
                                westLng = "-122.0306393";
                            };
                        };
                    }
                );
                "values_available" = 1;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_ROAD_ACCESS_INFO";
                value =                 (
                                        {
                        "access_info" =                         {
                            "road_access_point" =                             (
                                                                {
                                    drivingDirection = "ENTRY_EXIT";
                                    location =                                     {
                                        lat = "37.331697";
                                        lng = "-122.0306393";
                                    };
                                },
                                                                {
                                    isApproximate = 1;
                                    location =                                     {
                                        lat = "37.331716";
                                        lng = "-122.030757";
                                    };
                                    walkingDirection = "ENTRY_EXIT";
                                }
                            );
                        };
                    }
                );
                "values_available" = 1;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_PLACE_INFO";
                value =                 (
                                        {
                        "place_info" =                         {
                            center =                             {
                                lat = "37.3316851";
                                lng = "-122.0300674";
                            };
                            timezone =                             {
                                identifier = "America/Los_Angeles";
                            };
                        };
                    }
                );
                "values_available" = 1;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_ENTITY";
                value =                 (
                                        {
                        entity =                         {
                            "is_disputed" = 0;
                            name =                             (
                                                                {
                                    locale = "en_US";
                                    "string_value" = "1 Infinite Loop";
                                }
                            );
                            type = ADDRESS;
                        };
                    }
                );
                "values_available" = 1;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_ADDRESS";
                value =                 (
                                        {
                        address =                         {
                            "known_accuracy" = POINT;
                            "localized_address" =                             (
                                                                {
                                    address =                                     {
                                        formattedAddressLine =                                         (
                                            "1 Infinite Loop",
                                            "Cupertino, CA  95014-2083",
                                            "United States"
                                        );
                                        structuredAddress =                                         {
                                            administrativeArea = California;
                                            administrativeAreaCode = CA;
                                            areaOfInterest =                                             (
                                                "Apple Inc."
                                            );
                                            country = "United States";
                                            countryCode = US;
                                            dependentLocality =                                             (
                                                "Bay Area"
                                            );
                                            fullThoroughfare = "1 Infinite Loop";
                                            geoId =                                             (
                                            );
                                            locality = Cupertino;
                                            postCode = 95014;
                                            postCodeExtension = 2083;
                                            postCodeFull = "95014-2083";
                                            subAdministrativeArea = "Santa Clara";
                                            subLocality = "Bay Area";
                                            subThoroughfare = 1;
                                            thoroughfare = "Infinite Loop";
                                        };
                                    };
                                    locale = "en_US";
                                }
                            );
                        };
                    }
                );
                "values_available" = 1;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_AMENITIES";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_STYLE_ATTRIBUTES";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            },
                        {
                "cache_control" = CACHEABLE;
                "start_index" = 0;
                status = "STATUS_SUCCESS";
                timestampFirstSeen = "454307424.248136";
                ttl = 2592000;
                type = "COMPONENT_TYPE_BUSINESS_CLAIM";
                "values_available" = 0;
                version = 1;
                "version_domain" =                 (
                    apple,
                    fwdgeo,
                    US
                );
            }
        );
        "result_provider_id" = 6489;
        status = "STATUS_SUCCESS";
    };
}

您正在获取数据的文本转储,因为您正在使用 println。 CLPlaceMark 是一个数据结构。

将此代码添加到您的关闭代码中,一切就绪:

latitude = placemark.location.coordinate.latitude;
longitude = placemark.location.coordinate.longitude;

检查排序。您可能想要选择第一个和最后一个元素,并确定这是按递增还是递减置信度顺序排序的。 placemark[placemark.count -1] 将为您提供最后一个元素。