Ios MKPolyline在缩放时绘制奇怪的遗迹

Ios MKPolyline在缩放时绘制奇怪的遗迹,ios,swift,mapkit,mkpolyline,Ios,Swift,Mapkit,Mkpolyline,我在地图上画多段线。有时,当我缩放几次时,类似这样的东西会留在地图上: 这是我无法改进的iOS地图的特定功能吗 或者我的画法有问题 我的MKPolyline类: class MulticolorPolylineSegment: MKPolyline { var color: UIColor? class func colorSegments(forLocations locations: [Location]) -> [MulticolorPolylineSegment] {

我在地图上画多段线。有时,当我缩放几次时,类似这样的东西会留在地图上:

这是我无法改进的iOS地图的特定功能吗

或者我的画法有问题

我的MKPolyline类:

class MulticolorPolylineSegment: MKPolyline {
var color: UIColor?

class func colorSegments(forLocations locations: [Location]) -> [MulticolorPolylineSegment] {
    var colorSegments = [MulticolorPolylineSegment]()


    let red   = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)

    // RGB for Yellow (middle)
    let yellow = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)

    // RGB for Green (fastest)
    let green  = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)
    let (speeds, minSpeed, maxSpeed) = allSpeeds(forLocations: locations)

    // now knowing the slowest+fastest, we can get mean too
    let meanSpeed = (minSpeed + maxSpeed)/2

    for i in 1..<locations.count {
        let l1 = locations[i-1]
        let l2 = locations[i]
        print("dsd")
        var coords = [CLLocationCoordinate2D]()

        coords.append(CLLocationCoordinate2D(latitude: l1.latitude, longitude: l1.longitude))
        coords.append(CLLocationCoordinate2D(latitude: l2.latitude, longitude: l2.longitude))

        let speed = speeds[i-1]
        var color = UIColor.black

        if speed < minSpeed { // Between Red & Yellow
            let ratio = (speed - minSpeed) / (meanSpeed - minSpeed)
            let r = CGFloat(red.r + ratio * (yellow.r - red.r))
            let g = CGFloat(red.g + ratio * (yellow.g - red.g))
            let b = CGFloat(red.r + ratio * (yellow.r - red.r))
            color = UIColor(red: r, green: g, blue: b, alpha: 1)
        }
        else { // Between Yellow & Green
            let ratio = (speed - meanSpeed) / (maxSpeed - meanSpeed)
            let r = CGFloat(yellow.r + ratio * (green.r - yellow.r))
            let g = CGFloat(yellow.g + ratio * (green.g - yellow.g))
            let b = CGFloat(yellow.b + ratio * (green.b - yellow.b))
            color = UIColor(red: r, green: g, blue: b, alpha: 1)
        }

        let segment = MulticolorPolylineSegment(coordinates: &coords, count: coords.count)
        segment.color = color
        colorSegments.append(segment)
    }

    return colorSegments
}

fileprivate class func allSpeeds(forLocations locations: [Location]) -> (speeds: [Double], minSpeed: Double, maxSpeed: Double) {
    // Make Array of all speeds. Find slowest and fastest
    var speeds = [Double]()
    var minSpeed = DBL_MAX
    var maxSpeed = 0.0

    for i in 1..<locations.count {
        let l1 = locations[i-1]
        let l2 = locations[i]

        let cl1 = CLLocation(latitude: l1.latitude, longitude: l1.longitude)
        let cl2 = CLLocation(latitude: l2.latitude, longitude: l2.longitude)

        let distance = cl2.distance(from: cl1)
        let time = l2.timestamp.timeIntervalSince(l1.timestamp as Date)
        let speed = distance/time

        minSpeed = min(minSpeed, speed)
        maxSpeed = max(maxSpeed, speed)

        speeds.append(speed)
    }

    return (speeds, minSpeed, maxSpeed)
}

}

有关此问题的任何更新@DCDC?@DaSilva遗憾的是,没有更新,可能一些自定义绘图可以解决此问题,但尚未对其进行测试。我一直在期待解决方案谢谢你回答@DCDC。现在我们两个都在找同样的东西。把我也算在内。我得到了同样的问题:
@objc(mapView:rendererForOverlay:) func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {


    let polyline = overlay as! MulticolorPolylineSegment
    let renderer = MKPolylineRenderer(polyline: polyline)
    renderer.strokeColor = polyline.color
    renderer.lineWidth = 10
    return renderer
}

func polyline() -> MKPolyline {
    var coords = [CLLocationCoordinate2D]()

    let locations = self.locations
    for location in locations {
        coords.append(CLLocationCoordinate2D(latitude: location.latitude,
            longitude: location.longitude))
        print("dodalo")
    }

    return MKPolyline(coordinates: &coords, count: run.locations.count)
}