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