紧密缩放时折线消失了 [英] Polylines are disappearing when zoom closely

查看:92
本文介绍了紧密缩放时折线消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我的折线渲染器:

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

请帮助!

推荐答案

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

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

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

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

mapView.delegate = self

2)将叠加层添加到地图上.

2) Add your overlay to the map.

mapView.addOverlay(polyLine())

我在项目中使用coredata,因此,如果myLocations为空,则返回空MKPolyline()

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)我们可以从MKMapViewDelegate访问rendererFor.您可以更改折线的颜色和宽度.

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
}

这篇关于紧密缩放时折线消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆