【发布时间】:2020-04-24 14:44:02
【问题描述】:
我已经设置了一个自定义 mapView 并在我的视图控制器上全屏显示它。当 mapView 初始化时,它会检查位置授权,如果授权则转 showsUserLocation = true
我可以在我的控制台中证明这个标志在授权后被击中并变为 true,并且 mapView 将正确运行我的 centerViewOnUserLocation() 方法,但我仍然看不到地图上的蓝点位置。我正在我的 iPhone 上进行测试,而不是模拟器。以下是一些代码示例:
视图控制器
private lazy var mapView: EVMapView = {
let result: EVMapView = EVMapView()
view.addSubview(result)
result.delegate = self
EVConstraintHelper.anchor(view: result, options: .classic)
return result
}()
自定义地图视图
class EVMapView: MKMapView, EVLocationProtocol {
var locationManager: CLLocationManager?
init() {
super.init(frame: .zero)
checkLocationServices()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuth()
} else {
print("Auth Denied")
}
}
func setupLocationManager() {
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationAuth() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways:
showsUserLocation = true
centerViewOnUserLocation()
case .denied:
print("Auth Denied")
case .notDetermined:
locationManager?.requestWhenInUseAuthorization()
case .restricted:
print("Auth Restricted")
@unknown default:
print("Unkown default selected in \(#function)")
}
}
func centerViewOnUserLocation() {
guard let coordinate = locationManager?.location?.coordinate else { return }
let span = MKCoordinateSpan.init(latitudeDelta: 2, longitudeDelta: 2)
let region = MKCoordinateRegion.init(center: coordinate, span: span)
setRegion(region, animated: true)
}
}
我已经设置了 plist Privacy - Location When In Use Usage Description 消息,但不确定它会是什么。
【问题讨论】:
标签: ios swift mapkit core-location