如何通过使用经度和纬度获取代码

时间:2019-02-19 05:37:56

标签: ios swift4

我是google maps的新手。我的要求是,我必须使用用户的纬度和经度来获取加号。我能够获取纬度和经度。但是在那之后如何获取加号。我不知道那如果有人帮忙做到这一点,那就太好了。谢谢您。

        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
        }

    }

3 个答案:

答案 0 :(得分:0)

一种可能性是使用Google Places SDK并消费GMSPlace类。它包含plusCode属性。这里的挑战可能是从可以使用Geocoding API覆盖的坐标中获取GMSPlace对象,该对象可以选择将坐标作为参数,并提供place_id作为响应,可以是handed over to Places SDK

答案 1 :(得分:0)

我认为您可以轻松获得这种方式

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

答案 2 :(得分:0)

第二种可能性是使用this web service -已链接测试代码。您想做的事在plusEncodingLatLonWorks()下给出。如果Java对您来说是一门外语,请继续阅读。

将JSON正文“ {{latitude”:0,“ longitude”:0}”和内容类型标头为“ application / json”的Http发送到https://hd1-units.herokuapp.com/plus。加号代码将在返回json的plusCode键中。