ios - Swift: How to enable and disable annotations on map view? -
i have code displays annotations on map view. however, i'm trying have function can enabled or disabled when user taps uiswitch button. suggestions on how can achieve this? in advance!
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 toggles annotation... @ibaction func turnoffannotations(_ sender: uiswitch) { //code enables , disables annotation? if toggle.ison{ let visible = mymapview.visiblemaprect let inrect = mymapview.annotations(in: visible) 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) annotation: mkannotation in mymapview.annotations{ if (inrect.contains(anno as! anyhashable)){ mymapview.addannotation(annotation) } } } //how add removed annotations here?... }
use viewfor(annotation : mkannotation)
method :
swift 3
@ibaction func changeannotationsvisibility(_ sender: uiswitch) { let annotationvisible = sender.ison annotation in mymapview.annotations { mymapview.view(for: annotation)?.ishidden = !annotationvisible } }
Comments
Post a Comment