我正在使用Mapbox iOS SDK并尝试绘制没有geojson的折线。我试图用这种方法得到路线:
func calculateRoute() {
...
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)
Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first else { return }
self.showPreview(route: route)
}
}
然后我试图画一条路线。
func showPreview(route: Route) {
guard let steps = route.legs.first?.steps else { return }
var points = [CLLocationCoordinate2D]()
for step in steps {
points.append(step.maneuverLocation)
}
let line = MGLPolyline(coordinates: &points, count: UInt(points.count))
mapView?.addAnnotation(line)
}
它在地图视图上绘制一条折线。我可以使用两个委托方法(MGLMapViewDelegate)更改折线的颜色和宽度:
func mapView(_ mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat {
return 10
}
func mapView(_ mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
return .blue
}
但是我找不到在折线周围设置边框宽度和边框颜色的方法。有什么办法吗?
答案 0 :(得分:1)
看起来我有一个与您相似的用例(即不使用geojson),最终得到了类似的结果。通过将路线与MGLLineStyleLayer
关联,您可以控制线路的视觉参数。
func showPreview(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 = mapView.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 = NSExpression(forConstantValue: UIColor.blue)
lineStyle.lineWidth = NSExpression(forConstantValue: 3)
// Add the source and style layer of the route line to the map
mapView.style?.addSource(source)
mapView.style?.addLayer(lineStyle)
}
}
您要添加边框并控制其外观。如果您在Mapbox网站上查看以下示例:Line style Example,他们将通过创建第二个MGLLineStyleLayer
并将其插入第一个casingLayer
来完成您想要的操作。他们称第二层为let casingLayer = MGLLineStyleLayer(identifier: "polyline-case", source: source)
// Add your formatting attributes here. See example on website.
。这是他们的代码,因此您可以看到它的构成与第一层相同。
style.insertLayer(casingLayer, below: lineStyle)
然后他们将其插入第一行下方,并且由于其宽度较宽,因此显示为边框。
{
"requests":[
{
"id":"1",
"method":"PATCH",
"url":"https://graph.microsoft.com/v1.0/me/onenote/pages/1-98c2295df76a4067a6036efc6a8f6f74!84-f754d551-02d2-4416-af42-8fcc644f10e6/content",
"headers":{
"Content-Type":"application/json"
},
"body":[
{
"target":"title",
"action":"replace",
"content":"2 - Test1"
}
]
}
]
}
希望这会有所帮助。