Swift:位置管理员没有更新位置?

时间:2015-11-22 01:51:22

标签: ios

我正在尝试实施GoogleMaps,但我甚至无法让CLLocationManager工作。 didUpdateLocations方法未运行。这是我的控制者:

import UIKit
import CoreLocation
import GoogleMaps

...

class RootMapViewController: UIViewController {

  let locationManager = CLLocationManager()

  @IBOutlet weak var mapView: GMSMapView!

  override func viewDidLoad() {
    super.viewDidLoad()

    fetchLocation()

    ...
  }

  func addMapPin(coordinates: CLLocationCoordinate2D, title: String, description: String) {
    ...
  }

  private func fetchLocation() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
  }

  private func centerMapOn(userCoordinates: CLLocationCoordinate2D) {
    ...
  }

}

// MARK: CLLocationManagerDelegate

extension RootMapViewController: CLLocationManagerDelegate {

  func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedWhenInUse {
      print("gggggggggggggggggg") // <----------------- prints properly **** 
      locationManager.startUpdatingLocation()
      print("hhhhh")
      mapView.myLocationEnabled = true
      mapView.settings.myLocationButton = true
    }
  }

  // ISSUE ARISES HERE: *****************************************
  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("zzzzzzzzzzzzzzzzzzz") // <----------------- doesn't print ****
    let userCoordinates = manager.location!.coordinate
    centerMapOn(userCoordinates)
    locationManager.stopUpdatingLocation()
  }

}

我的Info.plist文件有密钥:

enter image description here

更新

enter image description here

1 个答案:

答案 0 :(得分:0)

你必须致电

locationManager.startUpdatingLocation; 

在fetchLocation()函数的最后一行。