使用注释坐标删除注释 [英] Removing Annotation using the Annotations Coordinates

查看:132
本文介绍了使用注释坐标删除注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase来获取mapView的注释。这可以通过以下方式完成:

I am using Firebase to get annotations on to mapView. This is done through the following:

 func getMarkers() {

    dbRef.observe(.value, with: { (snapshot) in

        for buildings in snapshot.children {

            let buildingsObject = Buildings (snapshot: buildings as! FIRDataSnapshot)

            let latDouble = Double(buildingsObject.latitude!)
            let lonDouble = Double(buildingsObject.longitude!)

            self.telephoneNumber = buildingsObject.number

            let annotation = myAnnotationView.init(title: buildingsObject.content!, coordinate: CLLocationCoordinate2D.init(latitude: latDouble!, longitude: lonDouble!), duration: buildingsObject.duration!, cost: buildingsObject.cost!, info: "", timestamp: buildingsObject.timestamp, contactNumber: buildingsObject.number!, addedBy: buildingsObject.addedByUser!)

            self.mapView.addAnnotation(annotation)

            print (snapshot)

        }})}

myAnnotationView是我的AnnotationView自定义类。

添加地图注释没问题。问题在于,如果用户需要删除其注释,则需要将其从mapView中删除。我有一个表格,其中包含所有用户注释,如果他们愿意,他可以删除。这将更新到firebase控制台,并删除数据。但是,注释仍然在地图上,只有当您重置应用程序时,注释才会更新。

myAnnotationView is my custom class for the AnnotationView.
adding Annotations to the map no problem. The issue arrises where if the user needs to remove his annotation it needs to be removed from the mapView. I have a table with all the users annotations in where he can delete if they so choose. This updates to firebase console and the data is removed. However, the annotation is still on the map and only when you reset the app the annotation will update.

我有一个方法来观察deletedChilds即可获得正确的快照但我似乎无法引用需要删除的注释。

I have a method to observe the deletedChilds which im getting a correct snapshot of but I cant seem to reference the annotation which needs to be removed.

func removeMarkers() {

    dbRef.observe(.childRemoved, with: { (snapshot) in

                        print (snapshot)

    })}

正在吐出的快照在这里:

the snapshot being spat out is here:

Snap (-KTo3kdGGA_-rfUhHVnK) { //childByAutoID
    addedByUser = TzDyIOXukcVYFr8HEBC5Y9KeOyJ2;
    content = "Post";
    cost = 500;
    duration = Monthly;
    latitude = "25.0879112000924";
    longitude = "55.1467777484226";
    number = 1234567890;
    timestamp = "Tue 11 Oct";
}

所以我的问题是如何删除此快照中的注释?我可以以某种方式引用它的坐标和removeAnnotation吗?我查看了Stack,但它主要提到了如何删除所有注释。

So my question is how can I remove the annotation which is in this snapshot? Can I somehow reference its coordinates and removeAnnotation that way? I have looked through Stack but its mostly mentioning how to remove all annotations.

非常感谢。 D

推荐答案

MapKit有removeAnnotation方法,可以用来删除特定的注释。

MapKit has removeAnnotation method, that can be used remove a specific annotation.

在您的情况下,您需要一种比较坐标的方法。我们可以使用CLLocationCoordinate2D上的扩展

In your case you need to a way to compare coordinates. WE can do that using an extension on CLLocationCoordinate2D

extension CLLocationCoordinate2D: Hashable {
    public var hashValue: Int {
        get {
            // Add the hash value of lat and long, taking care of overlfolow. Here we are muliplying by an aribtrary number. Just in case.
            let latHash = latitude.hashValue&*123
            let longHash = longitude.hashValue
            return latHash &+ longHash
        }
    }
}

// Conform to the Equatable protocol.
public func ==(lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
    return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}

现在,您可以获取所有注释从地图上,可以检查是否有任何匹配您的坐标&删除它。

Now, you can fetch all the annotations from the map, can check if any matches your co-ordinate & remove it.

        let allAnnotations = self.mapView.annotations
        for eachAnnot in allAnnotations{
            if eachAnnot.coordinate == <Your Coordinate>{
                self.mapView.removeAnnotation(eachAnnot)
            }
        }

这篇关于使用注释坐标删除注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆