2015-07-07 3 views
1

Что я хочу достичь, когда я нажал кнопку, аннотация покажет и скроется, если я снова нажму кнопку. Пока что он покажет только аннотацию.Показать/скрыть аннотацию при нажатии кнопки

Вот мой код.

@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

Используйте этот код для добавления и удаления аннотации:

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) 
    } 
} 
Смежные вопросы