gpt4 book ai didi

swift - 如何使用 mapbox : swift 用虚线绘制折线 (MGLPolyline)

转载 作者:可可西里 更新时间:2023-11-01 00:39:01 36 4
gpt4 key购买 nike

我试过下面的代码,但它画的是直线,我想要虚线

@IBOutlet var navigationView: MGLMapView!
var polyline: MGLPolyline!

override func viewDidLoad() {
super.viewDidLoad()

var mapCoordinates: [CLLocationCoordinate2D] = []
let newCoord1 = CLLocationCoordinate2D(latitude: 8.5241, longitude: 76.9366)
let newCoord2 = CLLocationCoordinate2D(latitude:11.8745, longitude: 75.3704)

mapCoordinates.append(newCoord1)
mapCoordinates.append(newCoord2)

polyline = MGLPolyline(coordinates: mapCoordinates, count: UInt(mapCoordinates.count))

navigationView.add(polyline)
}

最佳答案

一种选择是使用 MGLLineStyleLayer ,它有一个 lineDashPattern属性(property)。

This example显示如何添加虚线,但对于您的用例,它看起来更像:

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
var mapCoordinates: [CLLocationCoordinate2D] = []
let newCoord1 = CLLocationCoordinate2D(latitude: 8.5241, longitude: 76.9366)
let newCoord2 = CLLocationCoordinate2D(latitude:11.8745, longitude: 75.3704)

mapCoordinates.append(newCoord1)
mapCoordinates.append(newCoord2)

polyline = MGLPolyline(coordinates: mapCoordinates, count: UInt(mapCoordinates.count))

source = MGLShapeSource(identifier: "line", shape: polyline, options: nil)
style.addSource(source)

let layer = MGLLineStyleLayer(identifier: "line-layer", source: source)
layer.lineDashPattern = NSExpression(forConstantValue: [2, 1.5])
style.addLayer(layer)
}

然后根据需要更新 source 上的 shape 属性。

关于swift - 如何使用 mapbox : swift 用虚线绘制折线 (MGLPolyline),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376657/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com