我有以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()

}


和此代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    print(userLocation)
    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
                                                      longitude: userLocation!.coordinate.longitude, zoom: 12.5)

    self.mapView.camera = camera
    self.mapView.myLocationEnabled = true
    // self.mapView.settings.myLocationButton = true

    locationManager.stopUpdatingLocation()
}


这项工作早先可行,但是现在好像printManager根本不返回任何信息,因此甚至没有调用locationManager。代码中没有错误,我似乎无法弄清楚为什么未调用该函数。谁能发现我想念的明显东西?

谢谢

最佳答案

我只是实现了这一点,我要说的是我所做的

首先,您创建一个locationManager变量

let locationManager: CLLocationManager = CLLocationManager()

您必须在viewDidLoad()中请求权限

viewMap.delegate = self
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()


然后,我创建了扩展并实现了委托

extension MyView: CLLocationManagerDelegate {

  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
   //here you configure the locationManager

  locationManager.startUpdatingLocation()

    viewMap.isMyLocationEnabled = true
    viewMap.settings.myLocationButton = true
}

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    }

}


希望对您有所帮助,在这里可以正常工作。

关于ios - 使用Google Maps快速获取我的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38616456/

10-12 14:32