Katie H
Katie H

Reputation: 2293

Removing subviews from MapPin

Within my Swift app, I followed this tutorial to add custom pins and callouts to my mapview. My code is almost exactly the same: https://github.com/wircho/CustomMapViewCallout

Every few minutes, I am trying to refresh the map data, by clearing the current annotations and callouts.

My pin class is CustomPin, and my callout class is CustomCallout.

I have tried:

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

But this is not removing my pins. How do I remove my pin and callout subviews from my mapview?

Upvotes: 0

Views: 440

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

You could create a timer and set it to 5 minutes and then call a function to remove your pins.

var timer = NSTimer()

In your viewDidLoad set this row

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

Iterate through your annotations and remove them

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

Upvotes: 1

Related Questions