gpt4 book ai didi

ios - Swift:如何在 map View 上启用和禁用注释?

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:40 26 4
gpt4 key购买 nike

我有一段代码可以在 map View 上显示一些注释。但是,我正在尝试使用一些功能,当用户点击 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?...
}

最佳答案

使用 viewFor(annotation : MKAnnotation) 方法:

swift 3

@IBAction func changeAnnotationsVisibility(_ sender: UISwitch) {
let annotationVisible = sender.isOn

for annotation in myMapView.annotations {
myMapView.view(for: annotation)?.isHidden = !annotationVisible
}
}

关于ios - Swift:如何在 map View 上启用和禁用注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40568817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com