【问题标题】:How to get Address of selected location from google place picker marker如何从谷歌地点选择器标记获取所选位置的地址
【发布时间】:2019-01-10 09:58:12
【问题描述】:

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

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

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

在android google place picker 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)


}
}

【问题讨论】:

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


    【解决方案1】:

    只需从标记中获取位置并调用以下函数,该函数将返回返回地址字符串

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2019-09-23
    • 2017-12-03
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多