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

Posted

技术标签:

【中文标题】GeoCoder 使用地址获取经度和纬度【英文标题】:GeoCoder using address to get longitude and latitude 【发布时间】:2015-05-26 04:34:44 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

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

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

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

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

【讨论】:

以上是关于GeoCoder 使用地址获取经度和纬度的主要内容,如果未能解决你的问题,请参考以下文章

Android在服务中使用Geocoder获取位置地址

当互联网关闭时,如何将纬度,经度转换为地址?

CLGeocoder 反向地理编码失败,错误域=NSURLErrorDomain 代码=-1000 -

使用 google.maps.geocoder 检索位置的纬度/经度

利用百度API(js),怎样通过地址获取经纬度

使用地理编码器根据纬度和经度检索地址会导致延迟。安卓