【问题标题】:How to get full address from latitude and longitude on HERE MAP iOS如何从 HERE MAP iOS 上的纬度和经度获取完整地址
【发布时间】:2019-10-30 16:27:36
【问题描述】:

我想在这里地图 iOS 高级 sdk 中按纬度和经度获取完整地址。在 Android 中,我看到有使用 ReverseGeocodeRequest 按纬度和经度获取地址的选项,但我没有找到任何适用于 iOS 的东西。

目前,我正在从CLLocationCoordinate2D 获取地址,但我认为通过 HERE MAP sdk 获取地址会更好,因为我使用的是 HERE MAP 而不是 Apple MAP。我附上了下面的android代码。

GeoCoordinate vancouver = new GeoCoordinate(latitude,longitude);

        new ReverseGeocodeRequest(vancouver).execute(new ResultListener<Location>() {

            @Override

            public void onCompleted(Location location, ErrorCode errorCode) {

                try {

                    assetData.address = location.getAddress().toString().replace("\n", "");



                } catch (NullPointerException e) {

                    e.printStackTrace();

                }



            }

        });

【问题讨论】:

标签: ios objective-c swift here-maps-rest


【解决方案1】:

您必须使用 HERE iOS SDK 中的NMAAddress 类。

NMAAddress 提供文本地址信息,包括门牌号、街道名称、城市、国家、地区等。它包含有关地图上的地址或点的所有内容。 NMAPlaceLocation 类表示地图上可以检索其他属性的区域。这些附加属性包括 NMAAddress、唯一标识符、标签、位置、访问位置和位置的 NMAGeoBoundingBox

请查看文档部分Geocoding and Reverse Geocoding了解更多详细信息,包括示例代码。

【讨论】:

    【解决方案2】:

    您需要使用 Google API 从地理坐标中获取地址,请使用以下代码

    func getAddressFromLatLong(latitude: Double, longitude : Double) {
        let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR_API_KEY_HERE"
    
        Alamofire.request(url).validate().responseJSON { response in
            switch response.result {
            case .success:
    
                let responseJson = response.result.value! as! NSDictionary
    
                if let results = responseJson.object(forKey: "results")! as? [NSDictionary] {
                    if results.count > 0 {
                        if let addressComponents = results[0]["address_components"]! as? [NSDictionary] {
                            self.address = results[0]["formatted_address"] as? String
                            for component in addressComponents {
                                if let temp = component.object(forKey: "types") as? [String] {
                                    if (temp[0] == "postal_code") {
                                        self.pincode = component["long_name"] as? String
                                    }
                                    if (temp[0] == "locality") {
                                        self.city = component["long_name"] as? String
                                    }
                                    if (temp[0] == "administrative_area_level_1") {
                                        self.state = component["long_name"] as? String
                                    }
                                    if (temp[0] == "country") {
                                        self.country = component["long_name"] as? String
                                    }
                                }
                            }
                        }
                    }
                }
            case .failure(let error):
                print(error)
            }
        }
    }
    

    【讨论】:

    • 您当然不需要使用谷歌地图从坐标反向地理编码地址。 CLGeocoder 是内置的(CoreLocation),并且从 iOS5 开始就能够做到这一点。我还认为,除非您也使用他们的地图,否则不允许您使用 Google 地图进行反向地理编码。
    • 从第一点开始我同意,但从第二点开始我不同意,如果我们不使用 Google 地图,我们仍然可以使用 google API 调用从 Geo Cods 获取地址。没有问题,上面的代码会给你地址,只需添加你的api密钥,你就可以通过http调用Google Api得到结果。
    • 您的示例代码在 google map api 上,但我希望它在 map sdk 上。
    • @AsadAliChoudhry 我知道它在技术上可行,但它违反了 Google 的服务条款。第 3.2.4 (e) 节规定:No Use With Non-Google Maps. Customer will not use the Google Maps Core Services in a Customer Application that contains a non-Google map.Google Maps Platform Terms of Service
    猜你喜欢
    • 2012-03-13
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多