MapKit showUserLocation没有显示蓝色的位置标记。

问题描述 投票:0回答:1

我设置了一个自定义的地图视图,并在我的视图控制器上全屏显示。当地图视图被初始化时,它检查位置授权,如果获得授权,它就会变成 showsUserLocation = true

我可以在我的控制台中po出这个标志在授权后被打到并变成了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
    }()

自定义地图视图

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隐私-位置使用时的使用说明信息,不知道还会是什么。

ios swift mapkit core-location
1个回答
0
投票

我想明白了... 我为mapView使用了自定义的placemark注释,删除了默认的注释。从技术上讲,蓝色的当前位置标记是一个默认的注释,所以它也被删除了。

© www.soinside.com 2019 - 2024. All rights reserved.