iOS中使用GMSAutocompleteFetcherDelegate时如何查找经度和纬度

Abh*_*Jha 4 google-maps ios swift

根据 Place Autocomplete 文档,预测数组的对象没有位置信息。那么我应该如何获取经度和纬度的详细信息。下面是我的代码 -

extension ViewController: GMSAutocompleteFetcherDelegate {


func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {

    tableData.removeAll()

    for prediction in predictions{

        tableData.append(prediction.attributedFullText.string)

    }
    locationTable.reloadData()
}

func didFailAutocompleteWithError(_ error: Error) {
    print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)

Har*_*sar 5

didSelectRowAtIndexPath在表视图中使用以下方法。这样你就会得到纬度、纬度和地址。

我亲自使用并测试它100%有效。

某些语法可能会根据您的 swift 版本而改变。

import GoogleMaps

func getLatLongFromAutocompletePrediction(prediction:GMSAutocompletePrediction){

    let placeClient = GMSPlacesClient()

    placeClient.lookUpPlaceID(prediction.placeID!) { (place, error) -> Void in
        if let error = error {
             //show error
            return
        }

        if let place = place {
            place.coordinate.longitude //longitude 
            place.coordinate.latitude //latitude
            obj.attributedFullText.string //Address in string
        } else {
            //show error
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有需要,请随时询问。