简体   繁体   English

Swift:位置管理器未更新位置?

[英]Swift: Location manager not updating location?

I'm trying to implement GoogleMaps but I can't even get the CLLocationManager to work. 我正在尝试实现GoogleMaps,但什至无法使CLLocationManager正常工作。 The didUpdateLocations method isn't being run. didUpdateLocations方法未在运行。 This is my controller: 这是我的控制器:

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()
  }

}

My Info.plist file has the key: 我的Info.plist文件具有密钥:

在此处输入图片说明

Update: 更新:

在此处输入图片说明

You have to call 你必须打电话

locationManager.startUpdatingLocation; 

on the last line of fetchLocation() function. 在fetchLocation()函数的最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM