为什么MKMapView中的用户位置显示纬度0.0和经度0.0?

Why does user location in MKMapView shows latitude 0.0 and longitude 0.0?

所有关于stack overflow的问题好像都是针对Java和Android的。

我有以下使用 MKMapView 控件的代码。

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.showsUserLocation = true
    mapView.setUserTrackingMode(.followWithHeading, animated: true)

    let locationCoordinate = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
    currentLocationCoordinateRegion = MKCoordinateRegion(center: locationCoordinate, latitudinalMeters: 200, longitudinalMeters: 200)

    mapView.setRegion(currentLocationCoordinateRegion, animated: true)

    print("latitude:", mapView.userLocation.coordinate.latitude, "longitude:", mapView.userLocation.coordinate.longitude)

}

我使用的是真实设备,所以地图视图应该显示我的当前位置,打印语句应该打印我的当前位置,但我在地图视图上看到的是蓝色,打印结果显示纬度 0.0调试 window:

中的经度为 0.0

latitude: 0.0 longitude: 0.0

为什么会这样,我应该怎么做才能解决这个问题?

我在工具栏上还有一个条形按钮项,代码如下:

@IBAction func actionShow(_ sender: UIBarButtonItem) {

    print("latitude:", mapView.userLocation.coordinate.latitude, "longitude:", mapView.userLocation.coordinate.longitude)

    let locationCoordinate = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
    currentLocationCoordinateRegion = MKCoordinateRegion(center: locationCoordinate, latitudinalMeters: 200, longitudinalMeters: 200)

    mapView.setRegion(currentLocationCoordinateRegion, animated: true)

}

这会产生相同的结果。

您必须请求访问用户位置的权限,这是您在 viewDidLoad 中执行的第一个操作,然后在您获得权限后继续显示用户位置,按照 UIViewController 将让您继续

class ViewController: UIViewController {

    @IBOutlet var mapView: MKMapView!
    var locationManager: CLLocationManager?

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.setUserTrackingMode(.followWithHeading, animated: true)

        self.checkLocationAuthorization()
    }

    func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) {
        switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) {
        case .authorizedAlways, .authorizedWhenInUse:
            mapView.showsUserLocation = true
        case .notDetermined:
            if locationManager == nil {
                locationManager = CLLocationManager()
                locationManager!.delegate = self
            }
            locationManager!.requestWhenInUseAuthorization()
        default:
            print("Location Servies: Denied / Restricted")
        }
    }

}

extension ViewController: MKMapViewDelegate, CLLocationManagerDelegate {

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let region = MKCoordinateRegion(center: userLocation.coordinate, latitudinalMeters: 200, longitudinalMeters: 200)
        mapView.setRegion(region, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        self.checkLocationAuthorization(authorizationStatus: status)
    }

}

您还必须在 Info.plist 中包含以下属性,否则 locationManager 将不会继续请求授权。

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Message for AlwaysAndWhenInUseUsageDescription</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Message for AlwaysUsageDescription</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>Message for WhenInUseUsageDescription</string>