我使用下面的代码加载了视图,为用户开始的地图中心添加了自定义注释图标,以便用户滚动查看时始终可以看到其起点。
if let lat = curBlip.blip_lat, let lon = curBlip.blip_lon {
let mapCenter = CLLocationCoordinate2DMake(lat, lon)
let mapSpan = MKCoordinateSpanMake(0.01, 0.01)
let mapRegion = MKCoordinateRegionMake(mapCenter, mapSpan)
self.map.setRegion(mapRegion, animated: true)
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "Your Blips Location"
annotation.subtitle = "Subtitle Placeholder"
self.map.addAnnotation(annotation)
当视图加载时,我总是先加载该注释,然后在第一个注释之后将名为“ set”的布尔值设置为true,以确保其获取自定义图标。我遇到的问题是,即使我已将注释设置为显示优先级,但当我将地图移开时,注释也会消失。如何使该注释始终存在,还是有更好的方法设置不会消失的“这是地图的起点”圆圈?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if set {
return nil
} else {
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotationId")
view.image = UIImage(named: "locationArea50")
view.canShowCallout = true
view.displayPriority = .required
set = true
return view
}
然后,我将地图滚动了一点之后,我怀疑滚动足够远,系统不得不使它们重新出现,注释消失了。我认为这与注释分组的工作方式有关,但是蓝色注释很特殊,我希望它始终存在,这就是我认为displayPriority所做的。