MKMapView: Why are my MKPolyline(s) invisible?

Hi!

Years ago Apple created an MKMapView demo called KMLViewer. It includes a KMLParser that reads a KML file and converts the data to Map Kit classes.
Originally it was in Obj. C, but someone translated it to Swift, and put it here:
https://github.com/ooper-shlab/KMLViewer-Swift

I imported it directly into Xcode and made it read a KML file I have created.
It correctly finds 10 overlays in the form of MKPolyline and 11 annotations in the form of MKPointAnnotation, but they are not shown/displayed correctly.
The biggest problem is that the lines are totally missing. And when I look at the overlays I can't seem to find the 2 geographic points/positions/locations that describes each line.

I am not sure how to make Xcode print out the content of the MKPolyline(s) instances in a way I can include here as text, and it seems you can't attach a screenshot here either, but I have managed to put the screenshot here: transformation.dk/deling/screenshot-overlays-MKPolyline-2021-05-13.png
So you can see the content as I found it. It seems to correctly indicate that the MKPolyline consists of 2 geographic points, but I haven't been able to find the points themselves...
On the annotations I was able to find the point, in the Xcode debugger, in this way:

(lldb) po annotations[0].coordinate

I hope someone can help me understand what is going on, and how to find the relevant debugging/tracking information in Xcode.
I am a bit surprised that I had to specifically write ".coordinate" in "po annotations[0].coordinate" in order to find the geographic point of the annotation!

I am aware that the problem might also be the MKPolylineRenderer class or the mapView:rendererForOverlay: method, so I have included the references to those in KMLviewer below:

Code Block swift
override func createOverlayPathRenderer(_ shape: MKShape) -> MKOverlayPathRenderer? {
let polyLine = MKPolylineRenderer(polyline: shape as! MKPolyline)
return polyLine
}

(EDIT: PS: The following code was display incorrectly in the preview...:)
Code Block swift
func rendererForOverlay(_ overlay: MKOverlay) -> MKOverlayRenderer? {
// Find the KMLPlacemark object that owns this overlay and get
// the view from it.
for placemark in _placemarks {
if placemark.overlay === overlay {
return placemark.overlayPathRenderer
}
}
return nil
}


Answered by OOPer in 674766022
Seems the current KMLParser cannot detect the right styles for LineString from xml file.

You can set some fixed value in overlayPathRenderer:
Code Block
var overlayPathRenderer: MKOverlayPathRenderer? {
if _overlayPathRenderer == nil {
if let overlay = self.overlay {
_overlayPathRenderer = geometry?.createOverlayPathRenderer(overlay as! MKShape)
if let renderer = _overlayPathRenderer as? MKPolylineRenderer {
renderer.strokeColor = .blue
renderer.lineWidth = 2
}
style?.applyToOverlayPathRenderer(_overlayPathRenderer!)
}
}
return _overlayPathRenderer
}


Just repeat the answer from the other thread, to keep a reference here, before you close the thread:

Did you set color and line width for the poly line ?

You should do this in

Code Block
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let routePolyline = overlay as? MKPolyline {
let renderer = MKPolylineRenderer(polyline: routePolyline)
renderer.strokeColor = UIColor.blue.cgColor
renderer.lineWidth = 2
return renderer
}
return MKOverlayRenderer()
}


Get more details here:
https://stackoverflow.com/questions/58013956/mapkit-draw-poly-line-from-coordinates-swift-5
Accepted Answer
Seems the current KMLParser cannot detect the right styles for LineString from xml file.

You can set some fixed value in overlayPathRenderer:
Code Block
var overlayPathRenderer: MKOverlayPathRenderer? {
if _overlayPathRenderer == nil {
if let overlay = self.overlay {
_overlayPathRenderer = geometry?.createOverlayPathRenderer(overlay as! MKShape)
if let renderer = _overlayPathRenderer as? MKPolylineRenderer {
renderer.strokeColor = .blue
renderer.lineWidth = 2
}
style?.applyToOverlayPathRenderer(_overlayPathRenderer!)
}
}
return _overlayPathRenderer
}


Thank you very much to both of you!
Now it works!
I choose the solution where the code is put in the KMLparser and not in the main file. That seems more correct and as kind of a preparation for implementing detecting style elements inside Linestring. But since I don't have different styles on different lines, I haven't bothered enhancing the parser itself. I am still a beginner in Swift. :-)
MKMapView: Why are my MKPolyline(s) invisible?
 
 
Q