简体   繁体   English

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

[英]iOS11 user location doesn't show the blue marker

I am developing an app which asks user for location permission. 我正在开发一个要求用户获得位置许可的应用程序。 I have asked for all the following permission in my info.plist file 我已在我的info.plist文件中要求以下所有权限

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

My code is as follows 我的代码如下

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()
    }

}

The problem is i don't see the blue marker which shows the user current location but it shows the correct area which the user is in. If I scroll to some other position in the map and click the button whose action is centerMapbtnPressed , it doesn't work. 问题是我没有看到显示用户当前位置的蓝色标记,但是显示了用户所在的正确区域。如果我滚动到地图上的其他位置并单击操作为centerMapbtnPressed的按钮,则不会不行

Try setting showsUserLocation on your MKMapView to true . 尝试将MKMapView上的showsUserLocation设置为true Seems like you haven't done that yet. 似乎您还没有这样做。 (See the docs for more info) (有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM