简体   繁体   中英

how to get plus code by using latitude and longitude

I am new to google maps.My requirement, I have to get plus code using latitude and longitude of user.I am able to get latitude and longitude.But after that how to get plus code.I am not aware of that.If any one helps out todo this ,Would be very great.Thanks in advance.

        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
        }

    }

I think you can get this way easily

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

A Possibility is to use Google Places SDK and consume GMSPlace class. It contains plusCode property. Here a challenge could be to get GMSPlace object from coordinates which you can cover using Geocoding API which has an option to take coordinate as params and provide us with place_id in response which can be handed over to Places SDK .

A second possibility is using this web service -- test code linked. What you'd like to do is given under plusEncodingLatLonWorks(). If Java is a foreign language to your good self, read on.

Http post to https://hd1-units.herokuapp.com/plus with the JSON body '{"latitude": 0, "longitude": 0}' and a content-type header being 'application/json'. The plus code will be in the return json's plusCode key.

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