蓝点不会出现在地图上。CLLocation 管理器和 self.mapView.userLocation.coordinate 值之间似乎存在一些脱节。
CLLocation 管理器正确返回 Apple Campus 坐标,但 mapView.userLocation.coordinate 值返回纬度和经度的 0,0。
我已经调试了好几个小时。
更多信息如下:
在 viewDidAppear 和 viewDidLoad 中,我将用户的当前位置打印到控制台如下:
print(self.mapView.userLocation.coordinate)
这是在控制台中呈现的输出:
CLLocationCoordinate2D(纬度:0.0,经度:0.0)
这就是我的 MapViewController 的样子:
import UIKit
import CoreLocation
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var manager: CLLocationManager? = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// map stuff
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest
self.mapView.delegate = self
// print user's current location to console
print("user location in view did load:")
print(self.mapView.userLocation.coordinate)
}
override func viewDidAppear(_ animated: Bool) {
manager?.requestAlwaysAuthorization()
manager?.startUpdatingLocation()
self.mapView.showsUserLocation = true
self.mapView.userTrackingMode = .follow
// print user's current location to console
print("user location in view did appear:")
print(self.mapView.userLocation.coordinate)
animateMapToUserLocation()
}
}
笔记:
我已将相关的隐私消息添加到 Info.plist 文件中。
故事板中的 MapView 以实心圆圈连接到正确的 ViewController。
MapViewController 实例化如下:
让 storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 让 mapVC = storyboard.instantiateViewController(withIdentifier: "MapVC") as? MapViewController self.present(mapVC!,动画:真,完成:无)