【问题标题】:How to add border to MKPolyline如何为 MKPolyline 添加边框
【发布时间】:2020-03-07 17:01:12
【问题描述】:

有没有办法在苹果地图视图中为折线添加边框?

附言

只是想分享一个解决方案

【问题讨论】:

    标签: swift mkmapview polyline mkoverlay


    【解决方案1】:

    我的实现是在主线下方添加另一个折线叠加层,其 lineWidth 比主线要厚几个像素(我们可以说是您的边框宽度)。

    // Instantiate the main polyline
    let polylineObj = MKPolyline(coordinates: yourArrayOfCoords, count: yourArrayOfCoords.count)
    // Identifier to tell which is which
    polylineObj.title = "main"
    // Instantiate the border polyline
    let borderPolylineObj = MKPolyline(coordinates: yourArrayOfCoords, count: yourArrayOfCoords.count)
    // Add main polyline
    appleMapView.addOverlay(polylineObj)
    // Add border polyline below the main polyline
    appleMapView.insertOverlay(borderPolylineObj, below: polylineObj)
    

    然后在MKMapViewDelegate函数上:

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolylineRenderer(overlay: overlay)
        // Use different colors for the border and the main polyline
        renderer.strokeColor = overlay.title == "main" ? .blue : .red
        // Make the border polyline bigger. Their difference will be like the borderWidth of the main polyline
        renderer.lineWidth = overlay.title == "main" ? 4 : 6
        // Other polyline customizations
        renderer.lineCap = .round
        renderer.lineJoin = .bevel
        return renderer
    }
    

    附言

    如果您有更好的实现,请回答。这种实现成本很高,因为这实际上会使渲染的折线数量增加一倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多