0

I just wrote few lines of code and got stuck trying add a detail button to my annotation point, I don't know how. Does anyone know how to do that? The image below shows what I want to achieve. Thanks!

https://i.stack.imgur.com/kK1Ox.jpg

Im trying to get the big i in the circle in my annotation.

Right now I just have an annotation with a title and a subtitle, which looks like below.

https://i.stack.imgur.com/8yCQO.jpg

Also, if you know, when a user clicks on the i in the circle, how do i bring them to a new segue?

Thanks for your help.

import UIKit
import MapKit

class PropertyMasterViewConroller: UIViewController, MKMapViewDelegate {


    @IBOutlet weak var nMapView: MKMapView!
    @IBOutlet weak var testButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Location of Boston College
        let initLocation = CLLocationCoordinate2D(
            latitude: 42.335992,
            longitude: -71.167333
        )
        //Span of the Map
        let span = MKCoordinateSpanMake(0.1, 0.1)
        let region = MKCoordinateRegion(center: initLocation, span: span)
        nMapView.setRegion(region, animated: true)

        addAnnotations()

        testButton.setTitle("Test Button", forState: .Normal)

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav.png"), forBarMetrics: .Default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.translucent = true
    }

    // When user taps on the disclosure button you can perform a segue to navigate to another view controller
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if control == view.rightCalloutAccessoryView{
            println(view.annotation.title) // annotation's title
            println(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! {

        println("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
        }

        var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    }

    func addAnnotations() -> Void {

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


        //1070 Beacon
        let annotation = MKPointAnnotation()
        annotation.setCoordinate(CLLocationCoordinate2D (latitude: 42.345532,longitude: -71.109526))
        annotation.title = "1070 Beacon Street"
        annotation.subtitle = "Brookline, MA 02446"
        nMapView.addAnnotation(annotation)

        //1070 Beacon
        let location_18 = MKPointAnnotation()
        location_18.setCoordinate(CLLocationCoordinate2D (latitude: 42.347236,longitude: -71.15000))
        location_18.title = "18 Shepard Street"
        location_18.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_18)


        //1070 Beacon
        let location_26 = MKPointAnnotation()
        location_26.setCoordinate(CLLocationCoordinate2D (latitude: 42.358663,longitude: -71.146024))
        location_26.title = "26 Lincoln Street"
        location_26.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_26)

        //1070 Beacon
        let location_1280 = MKPointAnnotation()
        location_1280.setCoordinate(CLLocationCoordinate2D (latitude: 42.363639,longitude: -71.139366))
        location_1280.title = "1280 Soldiers Field Road"
        location_1280.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_1280)

        //1070 Beacon
        let location_59 = MKPointAnnotation()
        location_59.setCoordinate(CLLocationCoordinate2D (latitude: 42.363147,longitude: -71.201493))
        location_59.title = "59-85 Chapel Street"
        location_59.subtitle = "Newton, MA 02458"
        nMapView.addAnnotation(location_59)

        //1070 Beacon
        let location_1 = MKPointAnnotation()
        location_1.setCoordinate(CLLocationCoordinate2D (latitude: 42.309260,longitude: -71.134556))
        location_1.title = "1 Newton Place"
        location_1.subtitle = "Newton, MA 02458"
        nMapView.addAnnotation(location_1)
    }
}
emaadali
  • 11
  • 3
  • Check this one https://stackoverflow.com/questions/28225296/how-can-i-add-a-button-to-mkpointannotation/28226174#28226174 – Talha Q Apr 13 '15 at 16:39
  • Is the map view's delegate outlet in the storyboard connected to the view controller? If not, the delegate methods like viewForAnnotation won't get called. –  Apr 13 '15 at 16:40
  • Johnny, when you say call the method viewForAnnotation do you mean something like this? mapView(mapView, viewForAnnotation: annotation) Also, where would I put that, just under the comment // For adding button we have to use a method named as viewForAnnotation? Anna, what do you mean map view delegate outlet? I just control clicked from MKMapView on the story board to my view controller and added that as an outlet. – emaadali Apr 13 '15 at 17:52
  • @emaadali, See the picture in this question: http://stackoverflow.com/questions/9056535/how-to-display-mkoverlay-on-mkmapview. The delegate outlet (circle icon) should be connected (filled in) to the view controller -- **not just** the mapView outlet. Additionally, you do not call the viewForAnnotation. The map view itself will call it (but only if its delegate is set). –  Apr 13 '15 at 17:59

1 Answers1

0

I do not have access to your second image url but I assume you are looking something as follows. If not, leave comment below.

  func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

        if control == annotationView.rightCalloutAccessoryView {
            performSegueWithIdentifier("Detail", sender: self)
        }
    }

You could also refer to the following examples:

Community
  • 1
  • 1
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Hey, The second image is available here: http://imgur.com/L3VMe5l The problem mainly is that I dont have a disclosure button in the annotations yet. I just cant get the button to show up. – emaadali Apr 13 '15 at 16:37