在地图上显示用户位置无效

时间:2019-05-18 07:08:26

标签: ios swift core-location

如果我正在运行代码,则圆角的蓝点不会显示在地图上。在地图上可见的用户位置无效。我找不到这个问题。谁能找到问题?

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)
    }

}

非常感谢:)

2 个答案:

答案 0 :(得分:1)

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

mapView

OR

您还可以使用以下代码启用它:

mapView.showsUserLocation = true

答案 1 :(得分:0)

您必须在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:)时,您可以重用上述方法,如下所示:

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)
    }

}