Swift 如何在mapbox iOS中通过绘制的管线获取注释图像

Swift 如何在mapbox iOS中通过绘制的管线获取注释图像,swift,mapbox,Swift,Mapbox,我计算路线的代码如下: func calculateRoute(waypoints: [Waypoint], completion: @escaping (Route?, Error?) -> ()) { // Coordinate accuracy is the maximum distance away from the waypoint that the route may still be considered viable, me

我计算路线的代码如下:

func calculateRoute(waypoints: [Waypoint],
                    completion: @escaping (Route?, Error?) -> ()) {

    // Coordinate accuracy is the maximum distance away from the waypoint that the route may still be considered viable, measured in meters. Negative values indicate that a indefinite number of meters away from the route and still be considered viable.
    let startPoint = Waypoint(coordinate: currentLocation, coordinateAccuracy: -1, name: "Origin")
    var waypointsWithCurrentLoc = waypoints
    waypointsWithCurrentLoc.insert(startPoint, at: 0)

    let options = NavigationRouteOptions(waypoints: waypointsWithCurrentLoc, profileIdentifier: .automobile)
    // Generate the route object and draw it on the map
    _ = Directions.shared.calculate(options) { [unowned self] (waypoints, routes, error) in
        if error != nil{
            print("Error occured:", error)
        }
        else{
            self.directionsRoute = routes?.first
            // Draw the route on the map after creating it
            self.drawRoute(route: self.directionsRoute!)
        }
    }
}
我画路线的代码如下:

func drawRoute(route: Route) {
    guard route.coordinateCount > 0 else { return }
    // Convert the route’s coordinates into a polyline
    var routeCoordinates = route.coordinates!
    let polyline = MGLPolylineFeature(coordinates: &routeCoordinates, count: route.coordinateCount)

    // If there's already a route line on the map, reset its shape to the new route
    if let source = map.style?.source(withIdentifier: "route-source") as? MGLShapeSource {
        source.shape = polyline
    } else {
        let source = MGLShapeSource(identifier: "route-source", features: [polyline], options: nil)

        // Customize the route line color and width
        let lineStyle = MGLLineStyleLayer(identifier: "route-style", source: source)
        lineStyle.lineColor = MGLStyleValue(rawValue: #colorLiteral(red: 0.2796384096, green: 0.4718205929, blue: 1, alpha: 1))
        lineStyle.lineWidth = MGLStyleValue(rawValue: 8)

        // Add the source and style layer of the route line to the map
        map.style?.addSource(source)
        map.style?.addLayer(lineStyle)
    }
}

我的代码有什么问题?它返回一条较长的多段线,因为我正在计算一条优化路线,其中注释只是途中的一个停靠点。

答案是无法将Z索引控件添加到地图样式中,您必须创建一条多段线,然后在图层的顶部添加注释作为单独的MapStyle元素。

我发现不需要添加图层,您可以将其插入最后一个图层下。 因此,在drawRoute(route:route)功能中,必须将最后一行更改为:

if let style = mapView.style, let last = style.layers.last {
    mapView.style?.insertLayer(lineStyle, below: last)
}
else {
    mapView.style?.addLayer(lineStyle)
}
这看起来有点像hack,因为不确定最后一层是否就是带有注释的层,但是如果你在地图上只有一条路线,那么这个代码可能会起作用

此示例仅表示有快速简便的解决问题的可能性,但最终需要安全地解决问题