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

时间:2018-02-22 15:58:57

标签: ios swift google-maps

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

2 个答案:

答案 0 :(得分:3)

在表格视图的didSelectRowAtIndexPath中使用以下方法。所以你将获得纬度,纬度和地址。

  

我亲自使用它并测试它100%工作。

根据您的swift版本,某些语法可能会发生变化。

import GoogleMaps

func getLatLongFromAutocompletePrediction(record:GMSAutocompletePrediction){

    let placeClient = GMSPlacesClient()

    placeClient.lookUpPlaceID(record.placeID!) { (place, error) -> Void in
        self.hideActivity(self.view)
        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 :(得分:-1)

我建议您使用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
    }
}