【问题标题】:accessing current Location in google maps访问谷歌地图中的当前位置
【发布时间】:2019-01-22 22:01:32
【问题描述】:

我一直在查看 google 文档以获取当前位置,但我正在努力在 ViewDidLoad 中指定位置,正如您所见,我已将 Location 设置为 Syndey Australia。在指定当前位置的谷歌文档文档中,他们使用的是“默认位置。坐标”,我无法在我的代码中访问 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")

    }
}

【问题讨论】:

  • 确保您已正确配置您的项目。

标签: ios swift google-maps


【解决方案1】:

您在方法searchLocationPressed中添加了错误的代码,请更新如下。

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

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

【讨论】:

  • 谢谢,我想知道用户输入位置时如何将标记移动到他们指定的位置
  • @Menphis,很高兴为您提供帮助。
  • 你知道我怎样才能有 2 个搜索控制器来开始和结束旅程
  • 你明白我的意思吗
  • 我没听明白,你能用 UI 解释一下吗?请更新您想要的图像。
猜你喜欢
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2016-04-29
  • 2023-01-25
相关资源
最近更新 更多