【问题标题】:iOS11 user location doesn't show the blue markeriOS11 用户位置不显示蓝色标记
【发布时间】:2018-04-16 16:40:58
【问题描述】:

我正在开发一个应用程序,它要求用户提供位置许可。我已经在我的 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()
    }

}

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

【问题讨论】:

    标签: ios swift mapkit


    【解决方案1】:

    尝试将showsUserLocation 上的MKMapView 设置为true。好像你还没有这样做。 (更多信息请参见the docs

    【讨论】:

    • 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2011-08-30
    • 1970-01-01
    • 2019-04-17
    • 2018-03-10
    相关资源
    最近更新 更多