Show/Hide 按下按钮时的注释

Show/Hide annotation when button is pressed

我想要实现的是,当我按下按钮时,注释将显示,如果我再次按下按钮,注释将隐藏。到目前为止它只是重复显示注释。

这是我的代码。

@IBAction func showAnnotation(sender: AnyObject) {
    addAttractionPins()
}

func addAttractionPins() {
    let filePath = NSBundle.mainBundle().pathForResource("Attractions", ofType: "plist")
    let attractions = NSArray(contentsOfFile: filePath!)
    for attraction in attractions! {
        let point = CGPointFromString(attraction["location"] as! String)
        let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(point.x), CLLocationDegrees(point.y))
        let title = attraction["name"] as! String
        let typeRawValue = (attraction["type"] as! String).toInt()!
        let type = AttractionType(rawValue: typeRawValue)!
        let subtitle = attraction["subtitle"] as! String
        let annotation = AttractionAnnotation(coordinate: coordinate, title: title, subtitle: subtitle, type: type)
        mapView.addAnnotation(annotation)
    }
}

使用此代码添加和删除注释:

var annotationIsVisible = false

@IBAction func showAnnotation(sender: AnyObject) {
    if !annotationIsVisible {
        addAttractionPins()
        annotationIsVisible = true

    }else {
        Map.removeAnnotations(Map.annotations)
        annotationIsVisible = false
    }

}

func addAttractionPins() {

    let filePath = NSBundle.mainBundle().pathForResource("Attractions", ofType: "plist")
    let attractions = NSArray(contentsOfFile: filePath!)
    for attraction in attractions! {
        let point = CGPointFromString(attraction["location"] as! String)
        let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(point.x), CLLocationDegrees(point.y))
        let title = attraction["name"] as! String
        let typeRawValue = (attraction["type"] as! String).toInt()!
        let type = AttractionType(rawValue: typeRawValue)!
        let subtitle = attraction["subtitle"] as! String
        let annotation = AttractionAnnotation(coordinate: coordinate, title: title, subtitle: subtitle, type: type)
        mapView.addAnnotation(annotation)
    }
}