如何从 google 地点选择器标记获取所选位置的地址

How to get Address of selected location from google place picker marker

更新 - 找到了解决方案,如果有人在寻找相同的解决方案,这里是示例项目。这将从坐标和附近位置获取城市、国家/地区、邮政编码和 LocalAdress(subLocality)。 https://github.com/SwiftGuides/Google_Place_Picker

我在我的项目中实现了 google 地点选择器 API,这样我就可以移动选择器并获取附近的位置以及我的选择器所在的确切街道的位置。

我想要地点选择器自定义位置的地址,例如(未命名的道路,印度 Panchkula)而不是 (37°19'1464"N 122°01'74.724"W)。

在android google place picker API 当我们点击"select this address"自定义位置时,它显示一个弹出窗口并以字符串格式设置位置(未命名的道路, Panchkula, India) 而不是 (37°19'1464"N 122°01'74.724"W)

我想要那样的东西

这是当前状态的图片

https://ibb.co/HYD5TKP

请帮忙!!

import UIKit
import GooglePlaces
import GooglePlacePicker
import GoogleMaps

class ViewController: UIViewController,GMSPlacePickerViewControllerDelegate {

@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var placeNameLabel: UILabel!
@IBOutlet weak var coordinatesLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}


// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {


    // Dismiss the place picker, as it cannot dismiss itself.
    viewController.dismiss(animated: true, completion: nil)


    print("Place name \(place.name)")
    print("PostalCode\(place.addressComponents)")
    print("Place address \(place.formattedAddress)")
    print("Place attributions \(place.attributions)")

    placeNameLabel.text = place.formattedAddress



    //Get address Seperated like city,country,postalcode
    let arrays : Array = place.addressComponents!
    for i in 0..<arrays.count {

        let dics : GMSAddressComponent = arrays[i]
        let str : String = dics.type

        if (str == "country") {
            print("Country: \(dics.name)")
        }
        else if (str == "administrative_area_level_1") {
            print("State: \(dics.name)")
        }
        else if (str == "administrative_area_level_2") {
            print("City: \(dics.name)")
        }
        else if (str == "postal_code"){
            print("PostalCode:\(dics.name)")
            addressLabel.text = dics.name  // this is only to get postalcode of the selected nearby location

        }
    }
}



func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
    // Dismiss the place picker, as it cannot dismiss itself.
    viewController.dismiss(animated: true, completion: nil)

    print("No place selected")
}



@IBAction func pickPlaceButton(_ sender: Any) {
    let config = GMSPlacePickerConfig(viewport: nil)
    let placePicker = GMSPlacePickerViewController(config: config)

    //This delegate has to be called here to proper working of cancle and selected location feature , it is not mentioned in the official documentation
    placePicker.delegate = self

    present(placePicker, animated: true, completion: nil)


}
}

只需从标记中获取位置并调用下面的函数,该函数将 return 一个 return 地址字符串

func getPlaceAddressFrom(location: CLLocationCoordinate2D, completion: @escaping (_ address: String) -> Void) {
        let geocoder = GMSGeocoder()
        geocoder.reverseGeocodeCoordinate(location) { response, error in
            if error != nil {
                print("reverse geodcode fail: \(error!.localizedDescription)")
            } else {
                guard let places = response?.results(),
                let place = places.first,
                    let lines = place.lines else {
                        completion("")
                        return
                }
                completion(lines.joined(separator: ","))
            }
        }
    }