在Google地图中访问当前位置

时间:2019-01-22 22:01:33

标签: ios swift google-maps

我一直在查看google文档以获取当前位置,但是我正在努力在ViewDidLoad中指定位置,如您所见,我已将Location设置为Syndey Australia。在google doc文档中,当指定当前位置时,它们使用的是“ default location.coordinate”,我无法在我的代码中访问defaultLocation.coordinate。

这是文档的链接:https://developers.google.com/maps/documentation/ios-sdk/current-place-tutorial

这是viewDidLoad内部的代码

    GMSServices.provideAPIKey("AIzaSyARsPfNs54hrUAC4yKtHeu2jXfGjeA0b-E")
    GMSPlacesClient.provideAPIKey("AIzaSyARsPfNs54hrUAC4yKtHeu2jXfGjeA0b-E ")
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView

    // creating the marker //

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

这是我的扩展代码

 extension MapsViewController: CLLocationManagerDelegate {
     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     let location : CLLocation = locations.last!
     print("Location:\(location)")
     let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: zoomLevel)
     if mapView.isHidden {
        mapView.isHidden = false
        mapView.camera = camera
      } 
      else {
        mapView.animate(to: camera)
    }

}
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .restricted:
        // display the map the default location
        mapView.isHidden = false
    case .denied :
        print("User denied access to location")
        mapView.isHidden = false
    case .notDetermined :
        print("Location status not deteremined")
    case .authorizedAlways  : fallthrough
    case . authorizedWhenInUse :
        print("Location status is OK")

    }
}

1 个答案:

答案 0 :(得分:2)

您在方法searchLocationPressed中添加了错误的代码,请按照以下说明进行更新。

@IBAction func searchLocationPressed(_ sender: UIBarButtonItem) {
    let autocomplete = GMSAutocompleteViewController()
    autocomplete.delegate = self
    self.present(autocomplete, animated: true, completion: nil)
}

您的错误是您提交的是UISearchController而不是GMSAutocompleteViewController