简体   繁体   中英

GeoCoder using address to get longitude and latitude

I'm trying to extract the latitude and longitude from the address by using geocoder. Unfortunately when I run the code below, I get a really long list of information apple stores on all locations. Below is the code I'm using to print the placement.

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

All I want is the lat and long so I can save it. The output result is:

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";
    };
}

You are getting a text dump of the data because you are using println. CLPlaceMark is a data structure.

Add this code to your closure code and you are all set:

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

Check the sorting order. You may want to pick the first and last element and figure out if this is sorted by increasing or decreasing confidence order. placemark[placemark.count -1] will get you the last element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM