0

我正在为 iOS 开发一个应用程序,并且在我的地图视图上遇到了自定义注释的问题。

这是想法:

1)我将用户位置加载到地图,从这个位置我向我的后端服务器发送一个请求以检索所有附近的兴趣点。

2)我有一个自定义注释如下:

class CustomPointAnnotation: MKPointAnnotation {
    var imageName: String?
    var activeImageName : String?
    var trainCrossingId : Int?
    var notificationCount : UILabel = UILabel()
    var labelIsHidden : Bool = true
}

当我将注释添加到地图上时,我会动态设置notificationCount标签以使用从 Firebase 数据库中检索到的数据。

3) 用户能够增加/减少围绕他们当前位置的半径的大小。选择这个新半径后,我使用mapView.removeAnnotations(mapView.annotations)删除所有注释,然后使用所选半径从服务器检索到的新数据重新添加这些注释。

问题:

最初加载视图时,注释使用正确的标签集等按预期工作。

但是,一旦用户更新了半径并且我删除/添加了以前的注释,注释及其相应的标签就不会按预期显示。

例如,这是地图首次进入视图时的样子:

在此处输入图像描述

然后一旦我改变半径的大小: 在此处输入图像描述

预期数据是初始图像中显示的数据(首次进入视图时),但是当半径更新时,我的注释的 z 值不被尊重,并且notificationCount不正确(例如,在第二个中找到的第三个点图表不应有标签)。这很奇怪,因为我已经设置了打印语句和断点来监视每个注释,func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}并且值是正确的并且是我所期望的,但是视图没有显示这些值。

关于这里可能出现什么问题的任何想法?提前致谢!

编辑以包含以下 mapView 委托:

 // Custom annotation view
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if !(annotation is CustomPointAnnotation) {
            return nil
        }

        let reuseId = "trainPin"

        var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        }
        else {
            anView?.annotation = annotation
        }

        let cpa = annotation as! CustomPointAnnotation
        let icon : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
        icon.image = UIImage(named:cpa.imageName)
        icon.layer.zPosition = 1
        anView?.rightCalloutAccessoryView = cpa.annotationButton
        anView?.addSubview(cpa.notificationCount)
        anView?.addSubview(icon)
        anView?.frame = CGRect(x: 0, y:0, width:32, height:32)
        anView?.canShowCallout = true
        return anView
    }
4

1 回答 1

1

我相信anView将被重用,当您删除和添加注释时,该视图将具有许多子视图。尝试先删除子视图,然后再次添加:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "trainPin"

    var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    }
    else {
        anView?.annotation = annotation
    }

    let cpa = annotation as! CustomPointAnnotation
    let icon : UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
    icon.image = UIImage(named:cpa.imageName)
    icon.layer.zPosition = 1
    anView?.rightCalloutAccessoryView = cpa.annotationButton
    for view in anView?.subviews {
        view.removeFromSuperView()
    }
    anView?.addSubview(cpa.notificationCount)
    anView?.addSubview(icon)
    anView?.frame = CGRect(x: 0, y:0, width:32, height:32)
    anView?.canShowCallout = true
    return anView
}
于 2017-03-09T03:39:04.177 回答