我正在开发一个需要获取用户当前坐标的应用程序。我正计划通过CLLocationManager's didUpdateLocations方法进行此操作。由于某些原因,didUpdateLocations没有被执行。但是,看来locationManager.startUpdatingLocation()已成功调用。我在此站点上看到的其他可能解决方案中没有一个对我有用。我已经将NSLocationAlwaysUsage添加到info.plist中。这是我的全部代码:

import UIKit
import MapKit
import CoreLocation

var region: MKCoordinateRegion!

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {


@IBOutlet weak var map: MKMapView!


var locationManager = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters



self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()

    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse, .authorizedAlways:
        if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
        print("Updating location now")
        }
    case .notDetermined:
        locationManager.requestAlwaysAuthorization()
    case .restricted, .denied:
        print("User must enable access in settings")
        break
    }



    if (region == nil){

    }

    else {
    map.setRegion(region!, animated: true)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
}




func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  print("Got location")

  let userLocation:CLLocation = locations[0]
  let lat:CLLocationDegrees = userLocation.coordinate.latitude
  let long:CLLocationDegrees = userLocation.coordinate.longitude
  let currentPos:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)

  didUpdateRegion(position: currentPos)

  print(lat)
  print(long)

}


func didUpdateRegion(position: CLLocationCoordinate2D) {

  let span = MKCoordinateSpanMake(0.075, 0.075)
  region = MKCoordinateRegion(center: position, span: span)

}

func locationManager(_manager: CLLocationManager, didFailWithError error: Error) {
  print(error.localizedDescription)
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
  switch status {
  case .notDetermined:
      // If status has not yet been determied, ask for authorization
      manager.requestWhenInUseAuthorization()
      break
  case .authorizedWhenInUse:
      // If authorized when in use
      manager.startUpdatingLocation()
      break
  case .authorizedAlways:
      // If always authorized
      manager.startUpdatingLocation()
      break
  case .restricted:
      print("User must activate location services in settings")
      break
  case .denied:
      print("User must activate location services in settings")
      break
  default:
      break
  }
}

当我在模拟器和实际设备上运行此代码时,我得到通知以允许位置跟踪。接受之后,控制台将显示“立即更新位置”,但从不打印“位置信息”。谢谢您能就此问题发表看法,对于一般应用程序开发人员来说,我是新手。

编辑:我在整个代码中添加了全部内容,而不仅仅是我认为相关的部分。基本上,我正在尝试使地图上显示的区域跟随用户。我尝试通过每次didUpdateLocations函数触发时更新变量“region”来做到这一点。

最佳答案

我说得对吗,您只添加了一个键-NSLocationAlwaysUsage?
尝试将两个键都添加到Info.plist中:

  • 隐私-使用时的位置使用情况说明
  • 隐私-位置始终使用用法说明

  • 此外,如果实施此协议方法会怎样?
     func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
       print("\(error.localizedDescription)")
    }
    

    它打印什么吗?抱歉,我要发表评论,但是我没有足够的声誉。

    关于ios - swift -didUpdateLocations没有被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41794359/

    10-12 03:44