当放大时,多边形消失

问题描述 投票:1回答:1

我创建了七个不同的折线。但是,当我仔细放大时,其中一些消失了。为什么会这样呢?我该如何预防?

这是我的折线渲染器:

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)
}

请帮助!

ios swift mapkit mkoverlay mkpolyline
1个回答
2
投票

我在使用MKPolyline时遇到类似的问题,下面是解决此问题的方法。

1)确保在viewDidLoad()中具有mapView委托。

mapView.delegate = self

2)将叠加层添加到地图。

mapView.addOverlay(polyLine())

我在项目中使用coredata,因此,如果myLocations为空,则返回空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)我们可以从MKMapViewDelegate访问rendererFor。您可以更改折线的颜色和宽度。

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
}
© www.soinside.com 2019 - 2024. All rights reserved.