Polylines are disappearing when zoom closely
Asked Answered
D

1

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!

Dachau answered 11/2, 2019 at 13:33 Comment(2)
Try this: #20602268 and this: #40238870Materialize
Hello, I saw both of them unfortunately still disappearing :/Foamy
V
0

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
}
Veriee answered 28/2, 2020 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.