gpt4 book ai didi

swift - 如何在 mapbox iOS 中的绘制路线上获取注释图像

转载 作者:行者123 更新时间:2023-11-28 07:50:54 25 4
gpt4 key购买 nike

Annotations show under the drawn route

我计算路线的代码如下;

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)
}
}

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

最佳答案

我发现您可以将其插入到最后一层之下,而不是添加层。因此,在您的 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,因为不确定最后一层是否正是带注释的层,但如果 map 上只有一条路线,那么这段代码可能会起作用。

这个例子只代表有解决问题的快速和简单的可能性,但最终需要安全

关于swift - 如何在 mapbox iOS 中的绘制路线上获取注释图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49738441/

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