MapKit showUserLocation未显示蓝色位置标记

时间:2020-04-24 14:44:02

标签: ios swift mapkit core-location

我已经设置了一个自定义mapView并将其全屏显示在我的视图控制器上。初始化mapView时,它将检查位置授权,如果授予则转showsUserLocation = true

我可以在控制台中输入经过授权后会击中该标志并将其变为true的情况,并且mapView将正确运行我的centerViewOnUserLocation()方法,但是我仍然看不到地图上的蓝点作为位置。我在iPhone上而不是模拟器上进行测试。下面是一些代码示例:

视图控制器

    private lazy var mapView: EVMapView = {
        let result: EVMapView = EVMapView()
        view.addSubview(result)
        result.delegate = self
        EVConstraintHelper.anchor(view: result, options: .classic)
        return result
    }()

自定义MapView

class EVMapView: MKMapView, EVLocationProtocol {
    var locationManager: CLLocationManager?

    init() {
        super.init(frame: .zero)
        checkLocationServices()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuth()
        } else {
            print("Auth Denied")
        }
    }

    func setupLocationManager() {
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.desiredAccuracy = kCLLocationAccuracyBest
    }

    func checkLocationAuth() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse, .authorizedAlways:
            showsUserLocation = true
            centerViewOnUserLocation()
        case .denied:
            print("Auth Denied")
        case .notDetermined:
            locationManager?.requestWhenInUseAuthorization()
        case .restricted:
            print("Auth Restricted")
        @unknown default:
            print("Unkown default selected in \(#function)")
        }
    }

    func centerViewOnUserLocation() {
        guard let coordinate = locationManager?.location?.coordinate else { return }
        let span = MKCoordinateSpan.init(latitudeDelta: 2, longitudeDelta: 2)
        let region = MKCoordinateRegion.init(center: coordinate, span: span)
        setRegion(region, animated: true)
    }
}

我已经设置了plist隐私-使用中的位置使用情况描述消息,并且不确定是否还会使用。

1 个答案:

答案 0 :(得分:0)

我知道了...我为mapView使用了自定义地标注释,该注释删除了默认注释。从技术上讲,蓝色当前位置标记是默认注释,因此也将其删除。

相关问题