0

I'm trying to find out how can i put an info bubble inside the annotation view when i click a pin on a map.

In the view did load I load this code that shows me the map

        let latDelta:CLLocationDegrees = 0.01

        let longDelta:CLLocationDegrees = 0.01


        let evntLat : NSString = venueLat
        let evntLng : NSString = venueLng
        let latitute1:CLLocationDegrees =  evntLat.doubleValue
        let longitute2:CLLocationDegrees =  evntLng.doubleValue


        let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        let pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)


        let region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
        mappy.setRegion(region, animated: true)

        let pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)
        let objectAnnotation = MKPointAnnotation()
        objectAnnotation.coordinate = pinLocation
        objectAnnotation.title = "\(self.venumeNam)"
        self.mappy.addAnnotation(objectAnnotation)

Is there any way to put inside this annotation view a button? I've tried this

let annotationView = MKAnnotationView()
        let detailButton: UIButton = UIButton(type:UIButtonType.DetailDisclosure) as UIButton
        annotationView.rightCalloutAccessoryView = detailButton

But i cant figure out how to implement it on my code so it can show it. Any idea of how can i put this bubble and configure a function when someone clicks it?

FYI it's only one pin that i'm displaying here!

Updated from this answer

 class detailsViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mappy: MKMapView!
    // Here we add disclosure button inside annotation window
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView{
            print(view.annotation!.title) // annotation's title
            print(view.annotation!.subtitle) // annotation's subttitle

            //Perform a segue here to navigate to another viewcontroller
            // On tapping the disclosure button you will get here
        }
    }

    // Here we add disclosure button inside annotation window
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        print("viewForannotation")
        if annotation is MKUserLocation {
            //return nil
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
        }

        let button = UIButton(type: .DetailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    }



    func displayMarkers() -> Void
    {

        let latDelta:CLLocationDegrees = 0.01

        let longDelta:CLLocationDegrees = 0.01


        let evntLat : NSString = venueLat
        let evntLng : NSString = venueLng
        let latitute1:CLLocationDegrees =  evntLat.doubleValue
        let longitute2:CLLocationDegrees =  evntLng.doubleValue


        let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        let pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)


        let annotationView = MKAnnotationView()



        let region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
        mappy.setRegion(region, animated: true)

        let pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)
        let objectAnnotation = MKPointAnnotation()
        objectAnnotation.coordinate = pinLocation
        objectAnnotation.title = "\(self.venumeNam)"

        self.mappy.addAnnotation(objectAnnotation)

    }

   } 
Community
  • 1
  • 1
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • Check out this one http://stackoverflow.com/questions/28225296/how-to-add-a-button-to-mkpointannotation/28226174#28226174 – Talha Q Aug 20 '16 at 11:06
  • @Johnny yes i'm trying to do it this way but no luck until now.. it shows the name but not the button see my updated question – Konstantinos Natsios Aug 20 '16 at 11:07

0 Answers0