MapKit标注消失

15
我有一个纬度数组和一个经度数组,将它们添加到类型为CLLocationCoordinate2D的数组中。然后使用新数组在地图上注释多个点。其中一些或大多数,甚至所有的注释都会显示在地图上,但当我放大地图时,一些注释会消失,然后再次出现,或者不出现。有没有什么办法让它们全部可见?这是我期望在缩小地图时出现的行为,而不是在放大时。

这是我用于上述描述的代码。


这是我用于上述描述的代码。

import UIKit
import MapKit
import CoreLocation

class MultiMapVC: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var multiEventMap: MKMapView!

var latDouble = Double()
var longDouble = Double()
let manager = CLLocationManager()
var receivedArrayOfLats = [Double]()
var receivedArrayOfLongs = [Double]()
var locations = [CLLocationCoordinate2D]()


func locationManager(_ manager: CLLocationManager, didUpdateLocations uLocation: [CLLocation]) {
    let userLocation = uLocation[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.3, 0.3)
    let usersLocation = userLocation.coordinate
    let region:MKCoordinateRegion = MKCoordinateRegionMake(usersLocation, span)
    multiEventMap.setRegion(region, animated: true)
    manager.distanceFilter = 1000
    self.multiEventMap.showsUserLocation = true
}

func multiPoint() {

    var coordinateArray: [CLLocationCoordinate2D] = []
    print ("Received Longitude Count = \(receivedArrayOfLongs.count)")
    print ("Received Latitude Count = \(receivedArrayOfLats.count)")
    if receivedArrayOfLats.count == receivedArrayOfLongs.count {
        for i in 0 ..< receivedArrayOfLats.count {
            let eventLocation = CLLocationCoordinate2DMake(receivedArrayOfLats[i], receivedArrayOfLongs[i])
            coordinateArray.append(eventLocation)
            print (coordinateArray.count)
        }
    }

    for events in coordinateArray {
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: events.latitude, longitude: events.longitude)
        multiEventMap.addAnnotation(annotation)
        }
}


override func viewDidLoad() {
    super.viewDidLoad()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    multiPoint()
        }


override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    multiEventMap.removeFromSuperview()
    self.multiEventMap = nil

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

请分享您的viewFor注释方法的代码。 - PPL
1
我遇到了同样的问题,似乎只存在于iOS 11中。 - user1418084
我更新了代码,包括来自我的VC的所有代码。但是我没有viewForAnnotation方法。 - jvan
它仍然发生在iOS 13上。 - Ramis
5个回答

15
NiltiakSivad的解决方案有效,但会恢复到旧版iOS 10外观。如果您想在iOS 11上保留新的气球标记,而仅在旧版iOS版本中使用旧式图钉外观,则可以按照以下方式实现委托方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "annotationView"
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
    if #available(iOS 11.0, *) {
        if view == nil {
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
        view?.displayPriority = .required
    } else {
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
    }
    view?.annotation = annotation
    view?.canShowCallout = true
    return view
}

这实际上可能是苹果的一个错误,因为默认的显示优先级应该是.required(但显然不是!) - KPM
1
(顺便说一句,您不应该将另一个答案称为“上面”或“下面”,因为它们的顺序取决于它们的评级。相反,您应该写“NiltiakSivad的解决方案”。我会相应地编辑您的答案。) - KPM

11

来自Leszek Szary的被采纳的答案是正确的。

但是有些细节需要注意。有时候即使存在MKMarkerAnnotationView,它们也无法被渲染。

view.displayPriority = .required

已经设置。

您看到的是不同规则的组合。

  1. MKAnnotationViews会从地图顶部向底部呈现。(北在哪里并不重要)。
  2. 如果MapKit决定绘制重叠的MKAnnotationViews,那么靠近底部的MKAnnotationView会被绘制在顶部(因为它后面绘制)。
  3. 不仅MKAnnotationViewsMKMArkerAnnotationViews下方渲染的标题也需要空间。这些标题的呈现受到markerView.titleVisibility的影响。如果将markerView.titleVisibility设置为.visible(而不是默认值.adaptive),则此标题比稍后呈现的MarkerAnnotationView更强,即使稍后的MarkerAnnotationView具有displayPriority = .required。靠近底部的MarkerAnnotationView不会被呈现。
  4. 即使MarkerAnnotationView靠近顶部具有低的displayPriority,也会发生这种情况。因此,具有低displayPriority.titleVisibility = .visibleMarkerAnnotationView可以使靠近底部具有displayPriority = .requiredMarkerAnnotationView消失。

我不知道有关此行为的文档。这是我在iOS 12上进行实验的结果。我的描述已经简化了。


6

当MKMarkAnnotationView首次分配时,我设置了 annotationView.displayPriority = .required。通常这就够了,但每次重用单元格时都设置它可以解决我的问题。


5
我遇到了类似的问题。我最好的猜测是这与iOS 11检测引脚冲突的方式有关。实现自定义注释视图或恢复使用iOS 10固定引脚可以解决我的问题。
例如,实现以下内容应该可以修复你的代码:
class MultiMapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
   override func viewDidLoad() {
       super.viewDidLoad()
       mapView.delegate = self    
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
       guard let annotation = annotation as? MKPointAnnotation else { return nil }

       let identifier = "pin-marker"
       var view: MKAnnotationView

       if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
           dequeuedView.annotation = annotation
           view = dequeuedView
       } else {
           view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
       }
       return view
   }
}

如果这不起作用,有一个 displayPriority 属性值得研究,因为它负责帮助确定在不同的缩放级别下应该隐藏/显示哪些标记。更多信息请参见https://developer.apple.com/documentation/mapkit/mkannotationview/2867298-displaypriority
希望这可以帮到您。

这个可以!我注意到一个问题,就是这些引脚不能被选择。 - jvan

2
我在使用Xcode 10和iOS 12+作为我的部署目标时遇到了类似的问题。一位苹果员工在https://forums.developer.apple.com/thread/92839中发布的帖子建议在出列标记上切换.isHidden属性。这样做有所改善,但问题并没有完全解决。
result?.isHidden = true
result?.isHidden = false
return result

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接