gpt4 book ai didi

ios - 当前位置谷歌地图 swift ,无法访问defaultLocation.coordinate

转载 作者:行者123 更新时间:2023-11-30 10:51:51 25 4
gpt4 key购买 nike

我一直在查看谷歌文档来获取当前位置,但我很难在 ViewDidLoad 中指定位置,正如您所看到的,我已将位置固定设置为 Syndey Australia。在谷歌文档文档中指定当前位置时,他们使用“默认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")

}
}

最佳答案

我认为您错过了在 Info.plist 文件中添加关键 NSLocationWhenInUseUsageDescription

将此 key 添加到使用说明中。然后它应该显示一个 iOS 系统弹出窗口以允许位置访问。

关于ios - 当前位置谷歌地图 swift ,无法访问defaultLocation.coordinate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54380215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com