iOS - Swift - How to add a button to MKPointAnnotaton - MapKit -
currently have map view set 5 hardcoded locations shown on map pins. when user clicks on these pins callout show user has details of location. within these callouts trying include button user can press , takes them view controller.
below current code have, when tap on pins no button shown, have idea missing or need in order implement button?
here code locations , current user location.
import uikit import mapkit import corelocation import scenekit class secondviewcontroller: uiviewcontroller, mkmapviewdelegate, cllocationmanagerdelegate { @iboutlet weak var mapview: mkmapview! //location manager property let locationmanager = cllocationmanager() override func viewdidload() { super.viewdidload() mapview.delegate = self //find current location view loaded self.locationmanager.delegate = self //setting accuracy self.locationmanager.desiredaccuracy = kcllocationaccuracybest //so location services used within app self.locationmanager.requestwheninuseauthorization() //start location manager , find current location self.locationmanager.startupdatinglocation() //use blue pin show location user self.mapview.showsuserlocation = true //array of dictionaries store locations let locationdata = [ //radio city tower ["name": "radio city tower", "latitude": 53.4071551, "longitude": -2.980798600000071], //metropolitian cathedral ["name": "metropolitian catherdral", "latitude": 53.40494649999999, "longitude": -2.9684022999999797], //walker art gallery ["name": "walker art gallery", "latitude": 53.410068, "longitude": -2.979666], //liver buildings ["name": "liver buildings", "latitude": 53.405808, "longitude": -2.995859], //st george's hall ["name": "st george's hall", "latitude": 53.409277, "longitude": -2.979946] ] //object each dictionary in locations array var annotations = [mkpointannotation]() //populating map location pins dictionary in locationdata { let latitude = cllocationdegrees(dictionary["latitude"] as! double) let longitude = cllocationdegrees(dictionary["longitude"] as! double) let coordinate = cllocationcoordinate2d(latitude: latitude, longitude: longitude) let name = dictionary["name"] as! string let annotation = mkpointannotation() //apply annotation annotation.coordinate = coordinate annotation.title = "\(name)" annotations.append(annotation) } mapview.addannotations(annotations) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - location delegate methods func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { //gets current location let location = locations.last //gets center of last location let center = cllocationcoordinate2d(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) //creatng region - map zoom let region = mkcoordinateregion(center: center, span: mkcoordinatespan(latitudedelta: double(0.004), longitudedelta: double(0.004))) //apply map view region self.mapview.setregion(region, animated: true) //stop updating location self.locationmanager.stopupdatinglocation() let currentlocation = cllocationcoordinate2dmake(location!.coordinate.latitude, location!.coordinate.longitude) //adding annotation current user location var pinannotation = mkpointannotation() //set pin coordinate users current location pinannotation.coordinate = currentlocation //annotation title pinannotation.title = "current location" //annotation subtitle pinannotation.subtitle = "you here!" //add pin mapview mapview.addannotation(pinannotation) }
and here code button implementation within callout(does not work)
func mapview(mapview: mkmapview!, annotationview view: mkannotationview!, calloutaccessorytapped control: uicontrol!) { if control == view.rightcalloutaccessoryview { print(view.annotation?.title) print(view.annotation?.subtitle) //perform segue navigate viewcontroller } } func mapview(mapview: mkmapview!, viewforannotation annotation: mkannotation!) -> mkannotationview! { print("viewforannotation") if(annotation mkuserlocation) { return nil } let reuseid = "pin" var pinview = mapview.dequeuereusableannotationviewwithidentifier(reuseid) as? mkpinannotationview if(pinview == nil) { pinview = mkpinannotationview(annotation: annotation, reuseidentifier: reuseid) pinview!.canshowcallout = true pinview!.animatesdrop = true } var button = uibutton(type: .detaildisclosure) uibutton pinview?.rightcalloutaccessoryview = button return pinview }
i missing simple not sure what. new ios/swift development , mapkit please bear me. understand question has been asked multiple times however, still unable solve this. appreciated, thank you.
you making small mistake in following callout method explicitly unwrapped mkannotationview! , mkmapview!. here corrected 1 have tested it.
func mapview(mapview: mkmapview, viewforannotation annotation: mkannotation) -> mkannotationview? { print("viewforannotation") if(annotation mkuserlocation) { return nil } let reuseid = "pin" var pinview = mapview.dequeuereusableannotationviewwithidentifier(reuseid) as? mkpinannotationview if(pinview == nil) { pinview = mkpinannotationview(annotation: annotation, reuseidentifier: reuseid) pinview!.canshowcallout = true pinview!.animatesdrop = true } let button = uibutton() button.frame = cgrect(x: 0, y: 0, width: 30, height: 30) let image = uiimage(named: "img.png") button.setimage(image, forstate: .normal) pinview?.rightcalloutaccessoryview = button return pinview }
Comments
Post a Comment