Swift 如何从google place picker marker获取所选位置的地址

Swift 如何从google place picker marker获取所选位置的地址,swift,google-maps,google-places-api,Swift,Google Maps,Google Places Api,更新-找到了解决方案,如果有人在寻找相同的解决方案,这里是示例项目。这将从坐标和附近位置获取城市、国家、邮政编码和本地地址(次本地)。 我已经在我的项目中实现了GooglePlacePicker API,这样我就可以移动picker并获得附近的位置,以及我的picker所在的确切街道的位置 我要的是取货人定制地点的地址,如(印度潘什库拉未命名道路),而不是(37°19'1464“N 122°01'74.724”W) 在android google place picker API中,当我们单击

更新-找到了解决方案,如果有人在寻找相同的解决方案,这里是示例项目。这将从坐标和附近位置获取城市、国家、邮政编码和本地地址(次本地)。

我已经在我的项目中实现了GooglePlacePicker API,这样我就可以移动picker并获得附近的位置,以及我的picker所在的确切街道的位置

我要的是取货人定制地点的地址,如(印度潘什库拉未命名道路),而不是(37°19'1464“N 122°01'74.724”W)

在android google place picker API中,当我们单击自定义位置的“选择此地址”时,它会显示一个弹出窗口,并以字符串格式(印度潘什库拉未命名道路)而不是(37°19'1464“N 122°01'74.724”W)设置位置

我想要那样的东西

以下是当前状态的图像

请帮忙

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)


}
}
导入UIKit
导入谷歌网站
导入GooglePlacePicker
导入谷歌地图
类ViewController:UIViewController、GMSplacePickerServiceController和Elegate{
@IBVAR地址标签:UILabel!
@IBVAR placeNameLabel:UILabel!
@IBVAR弱坐标标签:UILabel!
重写func viewDidLoad(){
super.viewDidLoad()
}
//要从位置选择器“self”接收结果,需要符合
//GMSPlacePickerServiceController删除并实现此代码。
func placePicker(uViewController:gmsPlacePickerServiceController,didPick地点:GMSPlace){
//取消位置选择器,因为它不能自行取消。
viewController.Disclose(动画:true,完成:nil)
打印(“地名\(Place.name)”)
打印(“PostalCode\(place.addressComponents)”)
打印(“地点地址\(Place.formattedAddress)”)
打印(“地点属性\(地点属性)”)
placeNameLabel.text=place.formattedAddress
//将地址分开,如城市、国家、邮政编码
让数组:Array=place.addressComponents!

对于0..中的i,只需从标记获取位置并调用下面的函数,该函数将返回返回地址字符串

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

Thanx明白了。如果有人在寻找相同的,这里是我制作的示例项目,用于获取城市、国家、邮政编码和本地地址(次本地性)