-1

i am working on a map app with some overlays (annotations, circles, polygons). And i also have UISwitches to appear/disappear them. For the annotation is easy: .add / .remove, it works.

@IBAction func airportLayerSwitch(_ sender: UISwitch) {

    if sender.isOn {
        mapView.addAnnotations(airports)
    } else {
        mapView.removeAnnotations(airports)
    }
}

but for the circles, polygons I can not make them disappear. Here is my func for the circle:

func airportBoundryOverlay(airportName:String, radius:CLLocationDistance){

    for airport in airports {
        if airport.title == airportName{
            let center = airport.coordinate
            let circle = MKCircle(center: center, radius: radius)
            mapView.addOverlay(circle)
        }
    }
}

func airportBoundries() {
    
    airportBoundryOverlay(airportName: "Békéscsaba",radius: 4000)
    airportBoundryOverlay(airportName: "Budaörs",radius: 4000)
}

so for my second UISwitch:

@IBAction func tizLayerSwitch(_ sender: UISwitch) {

    if sender.isOn {
        airportBoundries()
    } else {
        // TODO: ??????? disappear airportBoundries() ??????
    }
}

when I turn the UISwitch on, the circles appear, but I cannot make them disappear.

vadian
  • 274,689
  • 30
  • 353
  • 361

1 Answers1

1

You can use MKMapView.removeOverlays call to do this.

var circleOverlays: [MKOverlay] = []

func airportBoundryOverlay(airportName: String, radius: CLLocationDistance) {
    for airport in airports {
        if airport.title == airportName{
            let center = airport.coordinate
            let circle = MKCircle(center: center, radius: radius)
            mapView.addOverlay(circle)

            // Collect the circle overlays here
            circleOverlays.append(circle)
        }
    }
}

@IBAction func tizLayerSwitch(_ sender: UISwitch) {
    if sender.isOn {
        airportBoundries()
    } else {
        mapView.removeOverlays(circleOverlays)
        circleOverlays = []
    }
}
Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30