我设置了一个自定义的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
}()自定义MapView
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,但不确定它还会是什么。
发布于 2020-04-25 02:36:52
我想通了..。我对mapView使用了自定义的placemark注释,它删除了默认注释。从技术上讲,蓝色的当前位置标记是默认注释,因此也将其删除。
https://stackoverflow.com/questions/61411029
复制相似问题