Show Annotation Title and SubTitle in Custom MKPinAnnotationView
Asked Answered
W

1

1

I am using MKPinAnnotationView inside my App.

I am setting MapView object as delegate, and using this code for customising my AnnotationView

   func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            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!.image = UIImage(named:"store.jpg")
            pinView!.animatesDrop = true
            pinView!.pinTintColor = UIColor.darkGrayColor()
         }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }

I am getting custom AnnotationView as I required.However, I am missing the features of title and subtitle of MKPointAnnotation.

I wish to see title and subtitle for the grey dots.enter image description here

Watchcase answered 22/6, 2016 at 10:9 Comment(0)
W
1

I was overriding one func

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        mapView.deselectAnnotation(view.annotation, animated: true)
    }

I commented this function out and got the titles and subtitles.

Updated Code

/*
 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
 mapView.deselectAnnotation(view.annotation, animated: true)
 }
 */
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView.canShowCallout = true
    pinView.animatesDrop = true
    pinView.pinTintColor = UIColor.darkGrayColor()
    pinView.draggable = true
    pinView.accessibilityLabel = "hello"
    let btn = UIButton(type: .DetailDisclosure)
    pinView.rightCalloutAccessoryView = btn
    return pinView
}
Watchcase answered 23/6, 2016 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.