swift - Show/Hide annotation when button is pressed -
what want achieve when pressed button annotation show , hide if pressed button again. far show annotation repeatedly.
here code.
@ibaction func showannotation(sender: anyobject) { addattractionpins() } func addattractionpins() { let filepath = nsbundle.mainbundle().pathforresource("attractions", oftype: "plist") let attractions = nsarray(contentsoffile: filepath!) 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) } }
use code add , remove 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!) 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) } }
Comments
Post a Comment