Polylines are disappearing when zoom closely

二次信任 提交于 2020-03-24 07:21:12

问题


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!


回答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
}


来源:https://stackoverflow.com/questions/54631738/polylines-are-disappearing-when-zoom-closely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!