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

时间:2019-01-10 09:58:13

标签: swift google-maps google-places-api

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

我在项目中实现了Google Place Picker api,这样我就可以移动Picker并获取附近的位置以及我的Picker所在的确切街道的位置。

我希望使用地点选择器自定义位置的地址,而不是(37°19'1464“ N 122°01'74.724” W)之类的(未命名的路,Panchkula,印度)。

在android Google地点选择器api中,当我们单击“选择此地址”作为自定义位置时,它会显示一个弹出窗口,并以字符串格式(未命名的路,Panchkula,印度)设置位置,而不是(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)


}
}

1 个答案:

答案 0 :(得分:0)

只需从标记中获取位置,然后调用以下函数即可返回返回地址字符串

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