sometimesiwritecode
sometimesiwritecode

Reputation: 3223

Why can't I customize my MKAnnotationView?

In my code I am using Apple maps and correctly setting up a mapView and the delegate to self within a View Controller.

Within my ViewController I also have the following code within a for loop:

if userOnMapAlready == false {
    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: otherUserLocation.coordinate.latitude, longitude: otherUserLocation.coordinate.longitude)
    annotation.subtitle = otherUserUUID
    self.mapView.addAnnotation(annotation)

}

I also have the following delegate function in my view controller, and again am setting mapview.delegate = self within the View Controller:

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.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .purple
        }
        else {
            pinView!.annotation = annotation
        }

        return pinView

    }

Sadly when the pins are drawn they are all red, not purple like the viewForAnnotation would suggest they should be. What is the problem?! Is it something to do with the reuseId not matching the unique subtitle im a assigning to each MKPointAnnotation??

UPDATE: just realized that the delegate function viewForAnnotation is never being called - which would explain why the pins are not purple. Why would viewForAnnotation not be getting called???

Upvotes: 0

Views: 60

Answers (1)

l'L'l
l'L'l

Reputation: 47284

In the latest version of Swift the property would be:

pinView!.pinTintColor = .purple

or...

pinView!.pinTintColor = MKPinAnnotationView.purplePinColor()

https://developer.apple.com/reference/mapkit/mkpinannotationview

Upvotes: 0

Related Questions