gpt4 book ai didi

ios - Swift + didUpdateUserLocation 没有被调用

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:21 24 4
gpt4 key购买 nike

我无法调用 MKMapView委托(delegate)方法 didUpdateUserLocation .

到目前为止我做了什么:

  1. 添加框架 MapKit.framwork在项目中

  2. 在 View Controller 中导入框架 import MapKit

  3. 在plist文件中添加 key

    <key>NSLocationAlwaysUsageDescription</key>
    <true/>

  4. View Controller 中的代码

添加委托(delegate) didUpdateUserLocation检查位置的方法是否已更新但从未调用过。

// MARK: - MapView delegate methods

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
// Calling...
}

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
// Not getting called
}

// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()

locationManager.startUpdatingLocation()

self.mapView.delegate = self
self.mapView.showsUserLocation = true
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
}

我已经使用模拟器并更改模拟器自定义位置来检查它是否被调用?方法regionDidChangeAnimated被称为完美!

同样的事情适用于 iOS7。 Swift map 位置更新还需要做哪些额外工作?

编辑: Plist 键

enter image description here

也没有提示权限警报。

最佳答案

  1. 您必须保留对 CLLocationManager 的引用。
  2. .startUpdatingLocation()showsUserLocation = true 之前,您必须等待调用 locationManager(_:didChangeAuthorizationStatus:) 委托(delegate)方法。
  3. 根据 the document : NSLocationWhenInUseUsageDescription 值类型为字符串。

尝试:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()

self.mapView.delegate = self
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
}

// MARK: - LocationManager delegate methods

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Authorized, .AuthorizedWhenInUse:
manager.startUpdatingLocation()
self.mapView.showsUserLocation = true
default: break
}
}

// MARK: - MapView delegate methods


func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
println(userLocation)
// Not getting called
}

}

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com