从 MapPin 中删除子视图

Removing subviews from MapPin

在我的 Swift 应用程序中,我按照本教程将自定义图钉和标注添加到我的地图视图中。我的代码几乎完全一样:https://github.com/wircho/CustomMapViewCallout

每隔几分钟,我都会尝试通过清除当前注释和标注来刷新地图数据。

我的 pin class 是 CustomPin,我的标注 class 是 CustomCallout。

我试过:

for subview in self.view.subviews {
            if (subview is CustomPin) {
                print(subview)
                subview.removeFromSuperview()
            }
        }

但这并没有删除我的图钉。如何从我的地图视图中删除我的图钉和标注子视图?

您可以创建一个计时器并将其设置为 5 分钟,然后调用一个函数来删除您的图钉。

var timer = NSTimer()

在你的viewDidLoad设置这一行

// 300 is 5 minutes
timer = NSTimer.scheduledTimerWithTimeInterval(300, target: self, selector: Selector("removePins"), userInfo: nil, repeats: true)

遍历您的注释并将其删除

func removePins(){
    for annotation in mapView.annotations as [MKAnnotation] {
        mapView.removeAnnotation(annotation)
    }
}