首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当前位置谷歌地图swift,无法访问defaultLocation.coordinate

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

Stack Overflow用户
提问于 2019-01-27 00:17:07
回答 2查看 129关注 0票数 0

在谷歌文档文档中指定当前位置时,他们使用的是“默认location.coordinate”,我无法在我的代码中访问defaultLocation.coordinate。

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

这是viewDidLoad中的代码

代码语言:javascript
复制
    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

这是我的扩展代码

代码语言:javascript
复制
 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")

    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-01-27 02:54:22

我想您没有在Info.plist文件中添加密钥NSLocationWhenInUseUsageDescription。

然后,它应显示iOS系统弹出窗口,以允许位置访问。

票数 0
EN

Stack Overflow用户

发布于 2019-01-27 05:29:01

我已经阅读了你的问题,并检查了你给地图教程的链接。我发现您的viewDidLoad方法中缺少一些代码:

代码语言:javascript
复制
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self

placesClient = GMSPlacesClient.shared()

不像locationManager.delegate = self那样将委托赋给CLLocationManager的对象

我不认为你的分机会被称为(CLLocationManagerDelegate).

也叫

根据@Let's_Create的回答,info.plist应该有以下键来解释应用程序是如何使用它的。dataNSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54380215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档