Mapbox 标记MGLPolyLines

Mapbox 标记MGLPolyLines,mapbox,Mapbox,我正在尝试显示Piste(构建为多边形线)的名称。我使用以下Swift代码创建了MGLPolyLineFeatures: var pistes = [MGLShape]() ... let shape = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count)) shape.attributes["label"] = label // This is a dynamic label - th

我正在尝试显示Piste(构建为多边形线)的名称。我使用以下Swift代码创建了MGLPolyLineFeatures:

var pistes = [MGLShape]()
...
let shape = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
shape.attributes["label"] = label // This is a dynamic label - the name of the piste
pistes.append(shape)
稍后,我将pistes添加到我的mapView中:

let source = MGLShapeSource(identifier: "pistes", shapes: pistes, options: nil)
style.addSource(source)
let symbolLayer = MGLSymbolStyleLayer(identifier: "pistes", source: source)
symbolLayer.text = NSExpression(forKeyPath: "{label}")
...
mapView.style.insertLayer(symbolLayer, below: style.layer(withIdentifier: "com.mapbox.annotations.points")!)
此代码不起作用-我的行上没有看到任何标签。但是,如果我添加以下内容:

symbolLayer.text = NSExpression(forConstantValue: "Piste")
它工作-我所有的行都标有“Piste”

我做错了什么?如何正确地将项目名称指定给MGLPolyLineFeature对象?或者,如何在MGLSymbolStyleLayer中正确指定标签名称

注:我使用的是MGLIMANT符号层,因为我也在每个活塞中间显示一个符号,指示斜率的方向——这很好。


我正在使用Mapbox iOS SDK V5.3,所以经过多次尝试和错误,我终于找到了答案

解决方案是将
MGLPolylineFeatures
存储为
MGLPolylineFeatures
的数组(而不是
MGLShapes
的数组)

然后使用
MGLShapeSource(标识符:功能:选项:)
方法,而不是
MGLShapeSource(标识符:形状:选项:)

另外,属性名称周围不需要“{}”

工作代码如下所示:

var pistes = [MGLPolylineFeature]()
...
let shape = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
shape.attributes["label"] = label // This is a dynamic label - the name of the piste
pistes.append(shape)

...

let source = MGLShapeSource(identifier: "pistes", features: pistes, options: nil)
mapView.style.addSource(source)
let symbolLayer = MGLSymbolStyleLayer(identifier: "pistes", source: source)
symbolLayer.text = NSExpression(forKeyPath: "label")
...
mapView.style.insertLayer(symbolLayer, below: style.layer(withIdentifier: "com.mapbox.annotations.points")!)

嘿,普雷斯托,这些线根据它们的属性有唯一的标签

如果您仍然需要将形状源设置为形状而不是特征,以使其易于更新,您还可以使用MGLShapeCollectionFeature(形状:特征)将其分组为单个MGLShape。我也犯了同样的错误,您的解决方案挽救了我的一天。谢谢:)