【发布时间】:2016-11-12 23:22:03
【问题描述】:
我有一个在地图视图上显示一些注释的代码。但是,我试图拥有一些功能,当用户点击 UISwitch 按钮时可以启用或禁用它。关于我如何实现这一目标的任何建议?提前致谢!
let theHouse = MKPointAnnotation()
theHouse.coordinate = CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365)
theHouse.title = "The House, DC"
theHouse.subtitle = "New Jersey, DC"
myMapView.addAnnotation(theHouse)
myAnnotations.append(theHouse.title!)
//Switch that toggles the annotation...
@IBAction func turnOffAnnotations(_ sender: UISwitch) {
//Code that enables and disables the annotation?
if toggle.isOn{
let visible = myMapView.visibleMapRect
let inRect = myMapView.annotations(in: visible)
for annotation: MKAnnotation in myMapView.annotations{
if (inRect.contains(anno as! AnyHashable)){
myMapView.removeAnnotation(annotation)
}
}
} else {
let visible = myMapView.visibleMapRect
let inRect = myMapView.annotations(in: visible)
for annotation: MKAnnotation in myMapView.annotations{
if (inRect.contains(anno as! AnyHashable)){
myMapView.addAnnotation(annotation)
}
}
}
//How to add back the removed annotations here?...
}
【问题讨论】:
-
您只需要删除注释
-
我有一个删除注释的代码,但是如果我想让注释重新出现,有没有简单的方法呢?
-
只是重新添加它们?添加和删除它们是一个非常简单的过程
-
您可以使用
isHidden属性来隐藏和再次显示它们。下面看看我的回答。希望对你有帮助