Рисование полилинии с помощью Border Mapbox, iOS - PullRequest
0 голосов
/ 22 октября 2018

Я использую SDK Mapbox iOS и пытаюсь нарисовать ломаную линию без геойсона.Я попытался получить маршрут с помощью этого метода:

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
}

, но я не могу найти метод для установки ширины и цвета границы вокруг полилинии.Есть ли способ сделать это?

1 Ответ

0 голосов
/ 22 октября 2018

Похоже, у меня был похожий случай использования (т. Е. Я не использовал 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: Пример стиля линии они делают то, что вы хотите, создав вторую MGLLineStyleLayer и вставив ее ниже первой.Они называют второй слой casingLayer.Это их код, поэтому вы можете видеть, что он сформирован так же, как и первый слой.

let casingLayer = MGLLineStyleLayer(identifier: "polyline-case", source: source)
    // Add your formatting attributes here. See example on website.

Затем они вставляют его ниже первой строки и, поскольку он имеет большую ширину, отображается в виде рамки.

style.insertLayer(casingLayer, below: lineStyle)

Надеюсь, это поможет.

...