繁体   English   中英

如何使用经纬度获取加码

[英]how to get plus code by using latitude and longitude

我是谷歌地图的新手。我的要求,我必须使用用户的纬度和经度来获得加号代码。我能够获得纬度和经度。但是在那之后如何获得加号代码。我不知道。如果有的话一个人帮忙做这件事,会非常好。提前致谢。

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!

            lat = locValue.latitude
            long = locValue.longitude
            str1 = String(lat)
            str2 = String(long)
        }
        func processResponse(withPlacemarks placemarks: [CLPlacemark]?, error: Error?) {
         actobj.stopAnimating()

            if let error = error {
                locationtxt.text = "Unable to Find Address for Location"

            } else {
                if let placemarks = placemarks, let placemark = placemarks.first {
                    locationtxt.text = placemark.compactAddress
                    actobj.isHidden=true
                } else {
                    locationtxt.text = "No Matching Addresses Found"
                }
            }
        }
 @IBAction func locationbtn(_ sender: Any) {

        guard let latAsString: String = str1, let lat = Double(latAsString) else { return }
        guard let lngAsString: String = str2, let lng = Double(lngAsString) else { return }

        let location = CLLocation(latitude: lat, longitude: lng)

        geocoder.reverseGeocodeLocation(location) { (placemarks, error) in

            self.processResponse(withPlacemarks: placemarks, error: error)
        }

        actobj.isHidden = false
        actobj.startAnimating()
    }

        }

    extension CLPlacemark {

        var compactAddress: String? {
            if let name = name {
                var result = name

                if let street = thoroughfare {
                    result += ", \(street)"
                }

                if let city = locality {
                    result += ", \(city)"
                }
                if let country = country {
                    result += ", \(country)"
                }
                return result
            }
            return nil
        }

    }

我想你可以很容易地做到这一点

        let lat = "19.0760"
        let long = "72.8777"
        let email = "abc@test.com"
        let ApiURL = "https://plus.codes/api?address=\(lat),\(long)&email=\(email)"

        Alamofire.request(ApiURL).responseJSON { response in
            print("Result: \(response.result)")
            if let json = response.result.value {
                print("JSON: \(json)")
            }
            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                print("Data: \(utf8Text)")
            }
        }

一种可能性是使用Google Places SDK并使用GMSPlace类。 它包含 plusCode 属性。 这里的一个挑战可能是从坐标中获取 GMSPlace 对象,您可以使用地理编码 API覆盖该对象,该API可以选择将坐标作为参数,并为我们提供place_id作为响应,可以将其移交给 Places SDK

第二种可能性是使用这个网络服务——测试代码链接。 您想要做的是在 plusEncodingLatLonWorks() 下给出。 如果 Java 对你的好人来说是一门外语,请继续阅读。

使用 JSON 主体 '{"latitude": 0, "longitude": 0}' 和内容类型标题为 'application/json' 的 Http 帖子到https://hd1-units.herokuapp.com/plus 加码将在返回 json 的 plusCode 键中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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