我有一个用displayPriority = .defaultHight显示一些注释的MapView,以允许自动集群。
MapView还会显示默认显示优先级为required的当前用户位置。
这会导致我的注释被用户位置注释隐藏,当它们非常接近时。
我想通过将用户位置注释的显示优先级设置为defaultLow来更改此行为。
我尝试使用这种方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
let userView = mapView.view(for: annotation)
userView?.displayPriority = .defaultLow
return userView
}
return mapView.view(for: annotation)
}但是,userView总是为空,因此我对displayPriority所做的修改不会被应用。
您知道如何更改MKUserLocation注释视图的displayPriority吗?
发布于 2020-04-26 13:37:58
我花了几个小时试图通过定制默认的用户位置注释来解决这个问题,但无济于事。
相反,作为一种解决办法,我制作了自己的位置标记,并隐藏了默认的位置注释。下面是我的代码:
将注释变量添加到viewController中
private var userLocation: MKPointAnnotation?在viewDidLoad中,隐藏默认位置标记:
mapView.showsUserLocation = false在didUpdateLocations中更新位置
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.first else { return }
if self.userLocation == nil {
let location = MKPointAnnotation()
location.title = "My Location"
location.coordinate = userLocation.coordinate
mapView.addAnnotation(location)
self.userLocation = location
} else {
self.userLocation?.coordinate = userLocation.coordinate
}
}然后在viewFor annotation中自定义注释视图
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// user location annotation
let identifier = "userLocation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
(annotationView as? MKMarkerAnnotationView)?.markerTintColor = .blue
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
annotationView?.displayPriority = .defaultLow
return annotationView
}我将注释的displayPriority更改为.defaultLow,以确保它不会隐藏其他注释。
如果这有帮助,请告诉我!
https://stackoverflow.com/questions/57543273
复制相似问题