Multiple MKPolyline changes color after zoom level changes

461 views Asked by At

I have an incoming data of 25 lines per second. Each line has gps data and altitude data. These lines are parsed and converted into mkpolylines and add as an overlay to map. Each polyline has a different color according to the altitude level. So briefly if the plane is at 0 meters the color is white and if it is at 2000 meters it is dark red. I have a color code array which the altitude is getting its corresponding color code from that array.

My code works with no problem but as soon as the zoom level is changed and I browse around the map, the generated polylines colors are changed with the current altitudes corresponding color code.

My code is very simple..

This is what I use for the polylines:

     var polyArray : [CLLocationCoordinate2D] = [polyFirstLocation.coordinate,newLocation.coordinate]
     var polyLine = MKPolyline()
     polyLine =  MKPolyline(coordinates: &polyArray, count: 2)
     polyLine.title = "planePath"
     colorAltitude(parsedData[7].doubleValue)
     mapView.addOverlay(polyLine, level: MKOverlayLevel.AboveLabels)

and this is my renderer:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

if overlay is MKPolyline {
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        if overlay.title == "planePath" {
            polylineRenderer.strokeColor = NSColor(netHex: cCode)
            polylineRenderer.lineWidth = 2
        }
        return polylineRenderer
    }
        return nil
}

It works.. and here is the image.. working

but when I zoom in to maximum level and browse around and zoom out again the colors are changed to my last color code value. So somehow I need to my renderer not to redraw my polylines or while redrawing somehow keep their original colors.

enter image description here

My code is in swift but I would really appriciate if you have an idea even though it is with obj-C.. I am desperate tried everything for the last four days.

Thanks..

0

There are 0 answers