Search code examples
mapboxmapbox-ios

How to add "greater than sign" line styles using mapbox-ios (example :>>>>)


I need a pointing line to tell someone the order of a line.

I added the dotted line style using MGLLineStyleLayer from mapbox-ios (example: - - - -) But I don't know if it supports the (>>>>) style, or the arrow (--->---), please tell me what to do.


Solution

  • You can create a line with an arrow by using the MGLineStyleLayer.linePattern property.

    First, create a UIImage with the pattern that you would like to use (in this case, a line with an arrow). Then add that image to your style using [MGLStyle setImage:forName]. That image can then be used for the line pattern.

        func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
            if let image = UIImage(named: "arrow.png") {
                style.setImage(image, forName: "arrow")
                let source = MGLShapeSource(identifier: "polyline", shape: shapeFromGeoJSON, options: nil)
                style.addSource(source)
    
                let layer = MGLLineStyleLayer(identifier: "polyline", source: source)
                layer.linePattern = NSExpression(forConstantValue: "arrow")
                layer.lineWidth = NSExpression(forConstantValue: 10)
                style.addLayer(layer)
            }
        }