KMLViewer (Map Kit): MKPointAnnotation text not displayed

Hi!

Here comes another question related to the KMLViewer MapKit example:

My KML file contains 11 annotations in the form of MKPointAnnotation.
Each of them contain a text, which can be found in '_title', in the Xcode debugger. But it is not displayed on the map (MKMapView).
Can any one help me figure out why?
Thank you!

The KMLViewer can be found here:
https://github.com/ooper-shlab/KMLViewer-Swift
It can be imported directly in Xcode.
Answered by OOPer in 674735022

Can any one help me figure out why?

As far as I tried KMLViewer with your kml shown in another thread of yours,
the title was shown in the callout when I tapped the pin.

I think this is the usual behavior of MKPinAnnotationView.
If you want to show the title sort of permanently, you may need to use another annotation view class.
For example:
Code Block
var annotationView: MKAnnotationView? {
if _annotationView == nil {
if let annotation = self.point {
if #available(iOS 11.0, *) {
let marker = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
_annotationView = marker
} else {
// Fallback on earlier versions
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.canShowCallout = true
pin.animatesDrop = true
_annotationView = pin
}
}
}
return _annotationView
}


Or else, if you do not prefer any of the predefined annotation view classs, you can create your own custom class by subclassing MKAnnotationView.
Accepted Answer

Can any one help me figure out why?

As far as I tried KMLViewer with your kml shown in another thread of yours,
the title was shown in the callout when I tapped the pin.

I think this is the usual behavior of MKPinAnnotationView.
If you want to show the title sort of permanently, you may need to use another annotation view class.
For example:
Code Block
var annotationView: MKAnnotationView? {
if _annotationView == nil {
if let annotation = self.point {
if #available(iOS 11.0, *) {
let marker = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
_annotationView = marker
} else {
// Fallback on earlier versions
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.canShowCallout = true
pin.animatesDrop = true
_annotationView = pin
}
}
}
return _annotationView
}


Or else, if you do not prefer any of the predefined annotation view classs, you can create your own custom class by subclassing MKAnnotationView.
Thank you very much!!
I can see now that MKMarkerAnnotationView has variables/properties to turn the displaying of a title and a subtitle on/off, but MKPinAnnotationView does not have such logic. (Strange.)
But... :-) There is still a problem:

Unless you zoom in, one or more MKPointAnnotation(s) are not shown at all, and on other MKPointAnnotation(s) the title text is missing. For my purpose, I want all information to be displayed all the time.

This subject - how many annotations to draw on the map - is described here:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html

Excerpts:

" All annotations are drawn at the same scale every time, regardless of
the map’s current zoom level. If your map contains many annotations,
your annotation views could overlap each other as the user zooms out. To
counter this behavior, you can add and remove annotations based on the
map’s current zoom level.
...
Implementing the logic necessary to add and remove annotations
is your responsibility.
...
The only way to eliminate annotation overcrowding is to remove some of
the annotation objects from the map view. This typically involves
implementing the mapView:regionWillChangeAnimated: and mapView:regionDidChangeAnimated:
methods to detect changes in the map zoom level. During a zoom change,
you can add or remove annotations as needed based on their proximity to
one another. You might also consider other criteria (such as the user’s
current location) to eliminate some annotations. "

According to that, it is the KMLviewer code that removes annotations based on zoom level. But where is that code? I can't find any 'regionWillChangeAnimated' or 'regionDidChangeAnimated' in the code.

Thank you!

According to that, it is the KMLviewer code that removes annotations based on zoom level. But where is that code?

NO. There's no such code in the KMLViewer. Maybe Apple's engineer included such functionality, described in the old archived document, into MKMapView itself.

Searching with mkmapview overlapping annotations I could find a good suggestion:
medium.com/swlh/how-to-stop-mapkit-annotation-clustering-790bcb7b8329
Code Block
let marker = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
marker.displayPriority = .required //<-
_annotationView = marker


Wauw! Yes - that works!
I would never have gotten that idea - that the documentation on MapKit - MKMapView - is so misleading...
Thank you!
Now all 'balloon' markers are shown all the time.
There is only one small inconvenience: Some of the title texts only appear when zooming. Do you know if there is any way of also forcing them to always be turned on?

I would never have gotten that idea - that the documentation on MapKit - MKMapView - is so misleading... 

Archived documentations will never be updated, even if they contained inappropriate description.
But the latest documents will give you some hints:

MKMarkerAnnotationView

...
Setting the Visibility var titleVisibility: MKFeatureVisibility
The visibility of the title text rendered beneath the marker balloon.
...

You can try something like this:
Code Block
let marker = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
marker.displayPriority = .required
marker.titleVisibility = .visible //<-
_annotationView = marker



Yes, that works perfectly! Thank you!

When I searched for information on displaying maps on iOS I found this:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/MapKit/MapKit.html
And the part about MKmapview NOT showing all annotations here:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW45

Now that you mention it, I can see that it might be outdated archived documentation. But that was what I found when I searched, and it doesn't refer you to any newer versions, so I assumed it was up-to-date.

But again: Thank you very much! Now it looks perfect.

KMLViewer (Map Kit): MKPointAnnotation text not displayed
 
 
Q