使用户位置在地图上可见不起作用

Make user location visible on the map does not work

如果我是 运行 我的代码,圆形蓝点不会显示在地图上。地图上可见的用户位置不起作用。我找不到问题所在。谁能找到问题?

import UIKit
import MapKit
import CoreLocation

class GPSNewBin: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var zurueckButton: UIButton!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        let locValue: CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
        let userLocation = locations.last
        let viewRegion = MKCoordinateRegion(center: (userLocation?.coordinate)!, latitudinalMeters: 600, longitudinalMeters: 600)
        self.mapView.setRegion(viewRegion, animated: true)
    }

}

非常感谢:)

您必须在 Info.plist 中包含以下属性,否则您的授权请求将无效。

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

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

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

viewDidLoad 中使用以下方法请求授权,它还会考虑到应用程序拒绝定位服务的情况。

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

checkLocationAuthorizationviewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    self.checkLocationAuthorization()
}

你还必须更新授权状态,当出现CLLocationManagerDelegate.locationManager(_:didChangeAuthorization:)时,你可以re-use上面的方法,像这样:

extension GPSNewBin: CLLocationManagerDelegate {

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

}

更新 didUpdateLocations 中的位置时,使用 userLocation.coordinate 中发送的更新后的用户位置,如下所示:

extension GPSNewBin: MKMapViewDelegate {

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

}

您可以从情节提要中启用用户位置:

您也可以使用代码启用它:

mapView.showsUserLocation = true