iOS11 用户位置不显示蓝色标记

标签 ios swift mapkit

我正在开发一个应用程序,要求用户提供位置许可。我已在我的 info.plist 文件中请求以下所有权限

Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description
Privacy - Location Always and When In Use Usage Description

我的代码如下

import UIKit
import MapKit
import CoreLocation

class MapVC: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    var locationManager = CLLocationManager()
    let authorizationStatus = CLLocationManager.authorizationStatus()
    let regionRadius: Double = 1000

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        mapView.delegate = self
        locationManager.delegate = self
        configureLocationServices()
    }

    @IBAction func centerMapbtnPressed(_ sender: Any) {
        if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse {
            centerMapOnUserLocation()
        }
    }
}

extension MapVC: MKMapViewDelegate {
    func centerMapOnUserLocation(){
        guard let coordinate = locationManager.location?.coordinate else {
            return
        }
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2, regionRadius * 2)

        mapView.setRegion(coordinateRegion, animated: true)
    }
}

extension MapVC: CLLocationManagerDelegate {

    func configureLocationServices(){
        if authorizationStatus == .notDetermined {
            locationManager.requestAlwaysAuthorization()
        }else {
            return
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        centerMapOnUserLocation()
    }

}

问题是我没有看到显示用户当前位置的蓝色标记,但它显示了用户所在的正确区域。如果我滚动到 map 中的某个其他位置并单击操作为 centerMapbtnPressed,不行。

最佳答案

尝试将 MKMapView 上的 showsUserLocation 设置为 true。好像你还没有这样做。 (有关详细信息,请参阅 the docs)

关于iOS11 用户位置不显示蓝色标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862287/

相关文章:

ios - 今天扩展 UIWebView

ios - 用指针转换 CFDictionaryRef?

ios - 更改 UIAlertView 中 UITextField 的键盘类型

ios - 使用超过 1 个 Prototype Cell 时 UITableView 的奇怪行为

ios - WkWebView 不全屏旋转视频?

ios - 不会为小距离位置更改调用 didUpdateLocations 委托(delegate)方法

iphone - 调整 iPhone 5 中的表格 View 大小

ios - AppCode 在编译前未检测到错误

ios - 如何使用自动布局在 MKAnnotation 中显示多行?

ios - 我应该在哪里将 MKPointAnnotation 添加到 MKMapView?