首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapKit showUserLocation未显示蓝色位置标记

MapKit showUserLocation未显示蓝色位置标记
EN

Stack Overflow用户
提问于 2020-04-24 22:44:03
回答 1查看 109关注 0票数 0

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

我可以在我的控制台中指出,这个标志在授权后被点击并变为true,mapView将正确运行我的centerViewOnUserLocation()方法,但我仍然看不到地图上的蓝点位置。我是在我的iPhone而不是模拟器上测试的。下面是一些代码示例:

视图控制器

代码语言:javascript
复制
    private lazy var mapView: EVMapView = {
        let result: EVMapView = EVMapView()
        view.addSubview(result)
        result.delegate = self
        EVConstraintHelper.anchor(view: result, options: .classic)
        return result
    }()

自定义MapView

代码语言:javascript
复制
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 Privacy - Location,但不确定它还会是什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-25 02:36:52

我想通了..。我对mapView使用了自定义的placemark注释,它删除了默认注释。从技术上讲,蓝色的当前位置标记是默认注释,因此也将其删除。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61411029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档