1

I created seven different polylines. However some of them are disappearing when I zoom in closely. Why it is happening? How can I prevent this?

Here is my polyline renderer:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
    renderer.strokeColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.9)
    renderer.lineWidth = 2.2
    return renderer
}

//Thousands of parameters sending as  a parameter
func createPathWithPoints(_ points: [MKMapPoint]) {
    let arc = MKPolyline(points: points, count: points.count)
    mapView.addOverlay(arc)
}

Please help!

Said Alır
  • 170
  • 2
  • 15
  • 1
    Try this: https://stackoverflow.com/questions/20601768/mkpolylinerenderer-produces-jagged-unequal-paths and this: https://stackoverflow.com/questions/40238370/mkpolyline-strange-rendering-related-with-zooming-in-mapkit – Glenn Posadas Feb 11 '19 at 14:03
  • Hello, I saw both of them unfortunately still disappearing :/ – Said Alır Feb 11 '19 at 14:21

1 Answers1

1

I had similar problem with using MKPolyline and below is what I did to fix this.

1) Make sure that you have your mapView delegate in viewDidLoad().

mapView.delegate = self

2) Add your overlay to the map.

mapView.addOverlay(polyLine())

I am using coredata in my project, so if myLocations are empty then I return empty MKPolyline()

private func polyLine() -> MKPolyline {
    guard let locations = myLocations else {
        return MKPolyline()
    }

    // Coordinates
    let coords: [CLLocationCoordinate2D] = locations.map { location in
        let location = location as! Location
        return CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
    }

    return MKPolyline(coordinates: coords, count: coords.count)
}

3) We have access to rendererFor from MKMapViewDelegate. You can change color and width for polyline.

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    guard let polyline = overlay as? MKPolyline else {
        return MKOverlayRenderer(overlay: overlay)
    }

    // Setup renderer
    let renderer = MKPolylineRenderer(polyline: polyline)
    renderer.strokeColor = .systemBlue
    renderer.lineWidth = 3

    return renderer
}
ShadeToD
  • 403
  • 9
  • 22