MapKit showUserLocation 未显示蓝色位置标记
MapKit showUserLocation is not showing blue location marker
我设置了自定义 mapView 并在我的视图控制器上全屏显示它。初始化 mapView 时,它会检查位置授权,如果授权则转 showsUserLocation = 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 消息,我不确定它还会是什么。
我想通了...我为 mapView 使用了自定义地标注释,删除了默认注释。从技术上讲,蓝色当前位置标记是默认注释,因此也被删除了。
我设置了自定义 mapView 并在我的视图控制器上全屏显示它。初始化 mapView 时,它会检查位置授权,如果授权则转 showsUserLocation = 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 消息,我不确定它还会是什么。
我想通了...我为 mapView 使用了自定义地标注释,删除了默认注释。从技术上讲,蓝色当前位置标记是默认注释,因此也被删除了。