Swift + didUpdateUserLocation没有被调用 [英] Swift + didUpdateUserLocation not getting called
问题描述
我无法调用MKMapView
委托方法didUpdateUserLocation
.
I am not able to call MKMapView
delegate method didUpdateUserLocation
.
我到目前为止所做的:
-
在项目中添加框架
MapKit.framwork
通过import MapKit
行
在plist文件中添加密钥
Add key in plist file
<key>NSLocationAlwaysUsageDescription</key>
<true/>
<key>NSLocationAlwaysUsageDescription</key>
<true/>
视图控制器中的代码
添加了委托didUpdateUserLocation
方法来检查位置是否已更新,但从未调用过.
Added delegate didUpdateUserLocation
method to check location is updated or not but never called.
// 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
被完美调用!.
I have used simulator and change simulator custom location to check whether it is called or not? The method regionDidChangeAnimated
is called perfectly!.
同样的东西适用于iOS7. Swift地图位置更新还需要付出哪些额外努力?
The same thing works for iOS7. what additional effort is remains for Swift map location update?
Plist键
也未提示权限警报.
推荐答案
- 您必须保留对
CLLocationManager
的引用. - 您必须在
.startUpdatingLocation()
和showsUserLocation = true
之前等待locationManager(_:didChangeAuthorizationStatus:)
委托方法被调用. - 根据文档:
NSLocationWhenInUseUsageDescription
值类型为String.
- You have to keep the reference to
CLLocationManager
. - You have to wait
locationManager(_:didChangeAuthorizationStatus:)
delegate method called, before.startUpdatingLocation()
andshowsUserLocation = true
. - According to the document:
NSLocationWhenInUseUsageDescription
value type is String.
尝试:
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
}
}
这篇关于Swift + didUpdateUserLocation没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!