2017-03-08 74 views
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标签使用,我已经从我的火力地堡数据库中检索数据。

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

的问题:

当最初加载视图,注释是否按预期工作与正确的标签设置等

然而,一旦用户更新半径和我删除/添加以前的注释,注释及其相应的标签未按预期显示。

例如,这是地图的外观时,首先进入的观点,如:

enter image description here

,然后一旦我改变半径的大小: enter image description here

预期的数据是初始图像中显示的内容(当首次进入视图时),但是当半径更新时,我的注释的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 
    } 
+0

您可以发布您的'FUNC MapView类(_ MapView类:的MKMapView,viewFor注释:MKAnnotation)代码 - > MKAnnotationView'? – chengsam

+0

当然,我刚编辑过将它添加到原始问题中。谢谢@chengsam – Dayna

回答

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 
} 
+0

这个伎俩!非常感谢chengsam,我非常感谢你的帮助 - 我不认为我会陷入子视图问题! – Dayna

+0

@Dayna我刚刚看到你以前的评论说这不起作用。你有什么不同吗? – chengsam

+0

是我的错,没有正确构建,因此代码没有更新哈哈!重建,它现在正在工作 - 再次感谢你,你节省了我吨的时间:) – Dayna