ios 如何使用MapBox在街道名称下绘制线条

irlmq6kh  于 5个月前  发布在  iOS
关注(0)|答案(2)|浏览(109)

我想用Mapbox画一条路线。我试过添加Map:

let polyline = MGLPolyline(coordinates: &coords, count: UInt(coords.count))
mapView?.add(polyline)

字符串
但是它一直画在街道名称的上面。我怎么能移到街道名称下面呢?

jhkqcmku

jhkqcmku1#

如果您将MGLPolyline直接添加到MGLMapView,则将其作为注解添加;目前Mapbox iOS SDK仅支持在所有内容之上添加注解。
然而,SDK有一个单独的API,称为runtime styling,它允许您将数据放置在Map的任何图层之下或之上。从this example,您可以使用以下代码将 shape source 添加到Map的样式和渲染形状源的 line layer。(MGLLineStyleLayer让人想起MapKit的MKOverlayPathRenderer。)

let routeFeature = MGLPolylineFeature(coordinates: &coords, count: UInt(coords.count))
let routeSource = MGLShapeSource(identifier: "route", shape: routeFeature, options: nil)
mapView.style?.addSource(routeSource)
let routeLayer = MGLLineStyleLayer(identifier: "route", source: routeSource)
// Set properties like lineColor, lineWidth, lineCap, and lineJoin
mapView.style?.insertLayer(routeLayer, above: roadLayer) // or below: labelLayer

字符串
如果你知道道路或标签图层的标识符,上面的代码就可以工作。你可以通过在Mapbox Studio中打开样式来获得标识符。为了更健壮,你可以覆盖Map样式中的所有图层,在你找到的第一个非符号图层上方插入路线图层。(标签和图标是使用符号图层渲染的。)

for layer in style.layers.reversed() {
    if !(layer is MGLSymbolStyleLayer) {
        style.insertLayer(routeLayer, above: layer)
        break
    }
}


顺便说一句,如果你需要的不仅仅是一条路线,Mapbox Navigation SDK for iOS附带了一个完整的逐向导航UI,包括一个为显示路线而优化的Map。

gblwokeq

gblwokeq2#

来这里购买Mapbox版本10的人可以使用下面的。
如果 street name layer 在line layer下面,请使用以下命令:
.withRouteLineBelowLayerId(“道路标签街道”)
如果 annotation layer 在line layer之下,请使用以下命令:
.withRouteLineBelowLayerId(“mapbox-android-pointAnnotation-layer-1”)

注意:这是Kotlin版本,但您可以使用层ID,因为它与您的情况相同,例如 react-native

相关问题