You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已授予定位权限后MapKit地图的定位Pin标记不显示该如何解决

MapKit定位标记不显示问题修复方案

存在的问题及修改点

  • 缺失Info.plist权限配置:你需要在项目Info.plist中新增键NSLocationWhenInUseUsageDescription,对应值填写权限申请的提示文本(如「需要使用您的位置信息展示当前定位」),无该配置时系统会直接拦截定位请求。
  • 授权状态回调为空实现:首次申请权限时,checkLocationAuth只在权限未确定时触发了申请操作,用户点击允许后的结果会走didChangeAuthorization回调,你当前该方法是空实现,拿到权限后不会自动开启用户位置展示。
  • 未启动位置更新:CLLocationManager配置完成后需要调用startUpdatingLocation()方法才会开始获取设备位置信息。
  • 未主动调整地图显示区域:开启showsUserLocation后地图默认不会跳转到用户位置所在区域,即使定位成功你也可能因为地图显示的是其他区域看不到标记,需要在拿到定位结果后主动设置地图的可见范围。

修复后的完整代码

import UIKit
import MapKit
import CoreLocation

class AmbulanceCallViewController: UIViewController {
    
    let rootMap:MKMapView = {
        let mapView = MKMapView()
        mapView.mapType = MKMapType.standard
        mapView.isZoomEnabled = true
        mapView.isScrollEnabled = true
        mapView.translatesAutoresizingMaskIntoConstraints = false
        return mapView
    }()
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(rootMap)
        checkLocationServices()
        NSLayoutConstraint.activate([
            rootMap.widthAnchor.constraint(equalTo: view.widthAnchor),
            rootMap.heightAnchor.constraint(equalTo: view.heightAnchor),
            rootMap.topAnchor.constraint(equalTo: view.topAnchor)
        ])
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

}

extension AmbulanceCallViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // 拿到最新定位后调整地图显示区域
        guard let userLocation = locations.last else { return }
        let region = MKCoordinateRegion(center: userLocation.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
        rootMap.setRegion(region, animated: true)
        // 只需要获取一次定位的话可以在这里停止更新
        locationManager.stopUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        // 授权状态变更后重新校验权限
        checkLocationAuth()
    }
}

extension AmbulanceCallViewController {
    func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
    
    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuth()
        } else {
            // 展示定位服务未开启的提示弹窗
        }
    }
    
    func checkLocationAuth() {
        print("Working on it...")
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse, .authorizedAlways:
            print("定位权限已获取")
            rootMap.showsUserLocation = true
            // 启动位置更新
            locationManager.startUpdatingLocation()
            break
        case .denied:
            print("定位权限被拒绝")
            break
        case .notDetermined:
            print("正在申请定位权限")
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            print("定位权限受限制")
            break
        @unknown default:
            fatalError("定位服务出现未知错误")
        }
    }
}

内容的提问来源于stack exchange,提问作者Farhandika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 01:39:01