【发布时间】: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