Mapkit Pin Annotations with Swift -
i trying display map of world several pins. when left button tapped, should perform phone call each pin associated different phone number. searched around , came following code calls same number. how can assign specific phone number each pin annotation?
import uikit import mapkit import corelocation class crewdesksviewcontroller: uiviewcontroller, mkmapviewdelegate { @iboutlet weak var mapview: mkmapview! @iboutlet weak var callcrew: uisegmentedcontrol! let locationmanager = cllocationmanager() override func viewdidload() { super.viewdidload() // ask authorisation user. self.locationmanager.requestalwaysauthorization() // use in foreground self.locationmanager.requestwheninuseauthorization() if cllocationmanager.locationservicesenabled() { // locationmanager.delegate = self locationmanager.desiredaccuracy = kcllocationaccuracynearesttenmeters locationmanager.startupdatinglocation() } //setup our map view self.mapview.delegate = self mapview.showsuserlocation = true // pin location of different phone numbers contacting crewdesk var location : cllocationcoordinate2d = cllocationcoordinate2dmake(50.1166667, 8.6833333) let pinannotation = pinannotation() pinannotation.setcoordinate(location) pinannotation.title = "frankfurt" pinannotation.subtitle = "+496969804620" self.mapview.addannotation(pinannotation) var location2 : cllocationcoordinate2d = cllocationcoordinate2dmake(41.8781136, -87.6297982) let pinannotation2 = pinannotation() pinannotation2.setcoordinate(location2) pinannotation2.title = "usa" pinannotation2.subtitle = "+1800fltline" self.mapview.addannotation(pinannotation2) var location3 : cllocationcoordinate2d = cllocationcoordinate2dmake(48.866667, 2.333333) let pinannotation3 = pinannotation() pinannotation3.setcoordinate(location3) pinannotation3.title = "paris" pinannotation3.subtitle = "+33800900814" self.mapview.addannotation(pinannotation3) var location4 : cllocationcoordinate2d = cllocationcoordinate2dmake(51.5085300, -0.1257400) let pinannotation4 = pinannotation() pinannotation4.setcoordinate(location4) pinannotation4.title = "london" pinannotation4.subtitle = "+44800896516" self.mapview.addannotation(pinannotation4) var location5 : cllocationcoordinate2d = cllocationcoordinate2dmake(50.833333, 4.333333) let pinannotation5 = pinannotation() pinannotation5.setcoordinate(location5) pinannotation5.title = "brussels" pinannotation5.subtitle = "+3980019326" self.mapview.addannotation(pinannotation5) var location6 : cllocationcoordinate2d = cllocationcoordinate2dmake(35.685, 139.7513889) let pinannotation6 = pinannotation() pinannotation6.setcoordinate(location6) pinannotation6.title = "japan 日本" pinannotation6.subtitle = "0-0531-12-4066" self.mapview.addannotation(pinannotation6) var location7 : cllocationcoordinate2d = cllocationcoordinate2dmake(22.2855200, 114.1576900) let pinannotation7 = pinannotation() pinannotation7.setcoordinate(location7) pinannotation7.title = "hongkong" pinannotation7.subtitle = "800-96-5483" self.mapview.addannotation(pinannotation7) var location8 : cllocationcoordinate2d = cllocationcoordinate2dmake(52.3740300, 4.8896900) let pinannotation8 = pinannotation() pinannotation8.setcoordinate(location8) pinannotation8.title = "amsterdam" pinannotation8.subtitle = "0-800-022-9324" self.mapview.addannotation(pinannotation8) var location9 : cllocationcoordinate2d = cllocationcoordinate2dmake(13.75, 100.516667) let pinannotation9 = pinannotation() pinannotation9.setcoordinate(location9) pinannotation9.title = "bangkok" pinannotation9.subtitle = "01-800-12-066-6078" self.mapview.addannotation(pinannotation9) var location10 : cllocationcoordinate2d = cllocationcoordinate2dmake(29.3375, 47.6581) let pinannotation10 = pinannotation() pinannotation10.setcoordinate(location10) pinannotation10.title = "kuwait" pinannotation10.subtitle = "00-1-847-700-9893" self.mapview.addannotation(pinannotation10) var location11 : cllocationcoordinate2d = cllocationcoordinate2dmake(1.3667, 103.8) let pinannotation11 = pinannotation() pinannotation11.setcoordinate(location11) pinannotation11.title = "singapore" pinannotation11.subtitle = "800-1204657" self.mapview.addannotation(pinannotation11) var location12 : cllocationcoordinate2d = cllocationcoordinate2dmake(-33.8830555556, 151.216666667) let pinannotation12 = pinannotation() pinannotation12.setcoordinate(location12) pinannotation12.title = "sydney" pinannotation12.subtitle = "1-800-1-48557" self.mapview.addannotation(pinannotation12) var location13 : cllocationcoordinate2d = cllocationcoordinate2dmake(25.0391667, 121.525) let pinannotation13 = pinannotation() pinannotation13.setcoordinate(location13) pinannotation13.title = "taipei" pinannotation13.subtitle = "0-0801-13-8533" self.mapview.addannotation(pinannotation13) } func mapview(mapview: mkmapview!, viewforannotation annotation: mkannotation!) -> mkannotationview! { if annotation pinannotation { let pinannotationview = mkpinannotationview(annotation: annotation, reuseidentifier: "mypin") pinannotationview.pincolor = .purple pinannotationview.draggable = true pinannotationview.canshowcallout = true pinannotationview.animatesdrop = true let deletebutton = uibutton.buttonwithtype(uibuttontype.custom) as! uibutton deletebutton.frame.size.width = 44 deletebutton.frame.size.height = 44 deletebutton.setimage(uiimage(named: "appiconround"), forstate: .normal) pinannotationview.leftcalloutaccessoryview = deletebutton return pinannotationview } return nil } func mapview(mapview: mkmapview!, annotationview view: mkannotationview!, calloutaccessorycontroltapped control: uicontrol!) { if let location = view.annotation as? pinannotation { uiapplication.sharedapplication().openurl(nsurl(string: "tel://+1800fltline")!) } }
you're using class pinannotation didn't provide definition of class.
i'll assume it's object conforms mkannotation protocol.
you're saving phone numbers in subtitle property of pinannotation objects.
in calloutaccessorycontroltapped method, passes annotation view user tapped.
the mkannotationview class has property annotation holds annotation object associated annotation view.
you need fetch annotation annotation view, load subtitle property (which part of mkannotation protocol) , use phone number place phone call.
Comments
Post a Comment