注释显示错误的图像

时间:2017-04-26 10:46:28

标签: swift swift3 mkmapview mkannotation mkannotationview

注释是否有可能显示其他图像然后应该使用?这是解决这个问题的方法吗?

我遇到了一个问题。在我的集群发展到单个注释之后,然后在更新(缩小 - 放大)之后,它们的图像将是不同的。在调试之后,它说针视图图像的颜色是我所使用的颜色。我可以在哪里查看确切的数据?也许在调用viewFor之后会发生一些事情。

这是我如何做到这一点的逻辑。

知道女巫模型的字典位于添加的注释的顶部。

var dicAnnotations = [FBAnnotation : MyModel]()

通过[MyModel]循环,我将数据附加到[FBAnnotation]的数组,这是通过FBClusteringManager保存它们所需的。然后在同时将新注释添加到[FBAnnotation]我添加

self.dicAnnotations[fbAnnotation] = objModel

知道哪个注释具有哪个对象。

因此,当我们知道如何记录注释数据时,如何将它们编入索引,让我们看一下MKMapView的委托。

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        DispatchQueue.global(qos: .userInitiated).async {
            let mapBoundsWidth = Double(self.mapView.bounds.size.width)
            let mapRectWidth = self.mapView.visibleMapRect.size.width
            let scale = mapBoundsWidth / mapRectWidth

            let annotationArray = self.clusterManager.clusteredAnnotations(withinMapRect: self.mapView.visibleMapRect, zoomScale:scale)
            for ann in annotationArray {
                if ann is FBAnnotation {
                    let a = ann as! FBAnnotation
                    print("Here u are , my color is : \(self.dicAnnotations[a]!.lineColor)")
                }
            }
            DispatchQueue.main.async {
                self.clusterManager.display(annotations: annotationArray, onMapView: self.mapView)
            }
        }
    }

for循环用于检查注释是否应该是 - 并且它是!

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        var reuseId = ""
        if annotation is FBAnnotationCluster {
            reuseId = "Cluster"
            var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
            if clusterView == nil {
                clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, configuration: FBAnnotationClusterViewConfiguration.default())
            } else {
                clusterView?.annotation = annotation
            }

            return clusterView
        } 

else {
            let ann = annotation as! FBAnnotation
            var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
                pinView = MKAnnotationView(annotation: ann, reuseIdentifier: "Pin")

                var modelType = dicAnnotations[ann]?.modelType
                modelType = modelType?.modelTypeToString(modelType: modelType!)
                let modelColor = dicAnnotations[ann]?.lineColor
                let modelDirection = dicAnnotations[ann]?.withHeading360
                print("\(modelType!)_\(modelColor!)")
                pinView?.image = UIImage(named: "\(modelType!)_\(modelColor!)")?.rotated(by: Measurement(value: modelDirection!, unit: .degrees))
            return pinView
        }
    }

它在堆栈调用中的表现如何:

Here u are , my color is : yellow
(lldb) po dicAnnotations[ann]
▿ Optional<MyModel>
  ▿ some : <Project.MyModel: 0x101b656f0>


(lldb) po dicAnnotations[ann]?.lineColor
▿ Optional<String>
  - some : "yellow"

来自:

// viewFor
... else { pinView?.annotation = ann  } ...

有什么想法吗?它显示我会说&#34;随机彩色图像&#34;。

提前致谢!

解决方案

我已编辑了代理人viewFor。删除了if pinView == null条件以及其他条件。不知怎的,它没有像它那样工作。

0 个答案:

没有答案
相关问题