Learn Courses My Dashboard

Customize MKMarkerAnnotationView

Using Chris’ Module 6 lesson 14 code, I tried to customize the MKMarkerAnnotation view with .glyphImage, which the Apple documentation has membership. But I get the message “Value of type ‘MKAnnotationView’ has no member ‘glyphImage’”. This is within a MKMarkerAnnotationView call. How can the MKMarkerAnnotationView be customized?

annotationView in your code is not a MKMarkerAnnotationView, it is a MKAnnotationView, which does not have the glyphImage property.

When you create a MKMarkerAnnotationView, you need to call it something else besides annotationView if you want to access those properties, as that variable has already been declared as type MKAnnotationView and you can’t change the type.

The boilerplate declarations are straight from module 6/14 and work OK. The “.canShow…” and “.rightCall…” adhere to the MKAnnotationView even though they are also within a MKMarkerAnnotationView declaration. I got that. It’s when I try to implement the two lines indicated that an error occurs since they are only MKMarkerAnnotationView compatible.
I’d still like to implement them somehow, but at a loss for how to go about it. Could you help with some code ideas? Do I need an external class definition? Delegation of some type?

The boilerplate declarations are straight from module 6/14 and work OK. The “.canShow…” and “.rightCall…” adhere to the MKAnnotationView even though they are also within a MKMarkerAnnotationView declaration. I got that. It’s when I try to implement the two lines indicated that an error occurs since they are only MKMarkerAnnotationView compatible.

MKMarkerAnnotationView inherits from MKAnnotationView. So when you declare a variable of type MKAnnotationView (which is what annotationView is), and then assign it a MKMarkerAnnotationView, you can only access the properties that it inherits from MKAnnotationView. That’s why you can use canShowCallout and rightCalloutAccessoryView but not glyphImage.

If you want to set MKMarkerAnnotationView properties, you need a MKMarkerAnnotationView variable.

Something like this might work (but note that I haven’t actually run it to find out):

var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: Constants.annotationReuseId)
if annotationView == nil {
    let markerView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constants.annotationReuseId)
    markerView.canShowCallout = true
    markerView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    let image = UIImage(named: "xyz.jpg")
    markerView.glyphImage = image
    annotationView = markerView
}

And for the future: Please post code as text, not screenshots. It is impossible to copy/paste screenshots to test and work out solutions. I had to type all that code above when if you had pasted text code I could have just copied yours, pasted it in and made whatever changes were needed.

When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

Worked beautifully. Thanks. And will comply in future posts.

is there a way to hide the marker? (red pin)

I’m trying to use a subview
annotationView!.addSubview(lbl)

and also hide the marker with alpha but it has been deprecated because it doesn’t work.
annotationView!.markerTintColor