【发布时间】:2021-02-13 19:41:33
【问题描述】:
我的视图中有一个 MapKit,我想访问 userLocation 数据。但是,实际功能
func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
print("it never gets called")
}
永远不会被调用。
整个代码如下所示:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
@IBOutlet weak var firstMapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
firstMapView.delegate = self
}
func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
print("it never gets called")
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("Authorized when in use detected")
locationManager.startUpdatingLocation()
firstMapView.showsUserLocation = true
}
}
}
当我运行代码时,我收到调试消息:print("Authorized when in use detected")
我看到this的问题,并试图复制东西,但没有帮助。
【问题讨论】:
-
您没有使用实际的委托方法:mapView(_:didUpdate:)
-
你能帮我正确的语法是什么吗?我看到了 Apple 文档,但不确定应该更改哪些部分,哪些不应该更改
标签: swift swiftui mapkit mkmapview core-location