我想要实现的是当我按下按钮时,注释将显示并且如果我再次按下按钮将隐藏。到目前为止,它只是反复显示注释。
这是我的代码。
@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)
}
}
答案 0 :(得分:0)
使用此代码添加和删除注释:
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)
}
}