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

4

根据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)
}

我不知道GMSAutocompleteFetcher是什么。你应该解释一下,并可能将相关标签添加到你的问题中。 - Duncan C
请查看我添加的以下答案。 - Harshal Bhavsar
2个回答

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
        }
    }
}

如有需要请随时提问。


1
始终欢迎。 - Harshal Bhavsar
谢谢,这个方法对我来说真是救星。 - Muhammad Danish Qureshi

-2
我建议您使用CoreLocation来获取纬度和经度的位置:
import CoreLocation
class YourViewController 
{

let locationManager = CLLocationManager()
var locValue: CLLocationCoordinate2D! = nil


    override func viewDidLoad() {
    super.viewDidLoad()

    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization()

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()
    }


    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
}

这里是委托

//MARK: - Location and alternative stuff

extension YourViewController : CLLocationManagerDelegate {

// MARK: Save actual position
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    self.locValue = locValue
    }
}

你的建议是诚实的,但不好,因为我在现场应用中使用这个东西时遇到了很严重的问题。苹果没有提供正确的经纬度信息,从谷歌地址获取地址然后从苹果获取经纬度会导致我们的位置偏离家。 - Muhammad Danish Qureshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接