func showTraffic(mapView: MapView) {
let sourceId = "mapbox-traffic-v1"
var vectorSource = VectorSource()
vectorSource.url = "mapbox://mapbox.mapbox-traffic-v1"
var trafficLayer = LineLayer(id: "traffic")
// `visibility` is `nil` by default. Set to `visible`.
trafficLayer.visibility = .constant(.visible)
trafficLayer.lineColor = .constant(StyleColor(.red))
trafficLayer.lineCap = .constant(.round)
trafficLayer.lineJoin = .constant(.round)
trafficLayer.lineWidth = .constant(5)
let style = mapView.mapboxMap.style
// Add the sources and layers to the map's style.
do {
try style.addSource(vectorSource, id: sourceId)
try style.addLayer(trafficLayer)
} catch {
print("Error when adding sources and layers: \(error.localizedDescription)")
}
}
I tried implementing this code, but nothing seems to happen. Do you have any ideas on how to display traffic on Mapbox?