Change color of polyline in static map image

616 Views Asked by At

I'm successfully creating a static image using MapboxStatic with the recorded track coordinates as GeoJson data.

I can't figure out how to edit the color of the route painted on the image - it defaults to a black/gray color and I can't find a way to change it.

I realize it must be by editing the GeoJson attributes itself but I have no idea how to it in swift?

Would love to understand how to change the color in some way...

       let camera = SnapshotCamera(
            lookingAtCenter: CLLocationCoordinate2D(latitude: self.mapView.camera.centerCoordinate.latitude, longitude: self.mapView.camera.centerCoordinate.longitude),
            zoomLevel: 12)
        let options = SnapshotOptions(
            styleURL: self.mapView.styleURL,
            camera: camera,
            size: self.mapView.bounds.size)
        
        let coordinates = self.viewModel.getTrack().locations?.map { loc -> CLLocationCoordinate2D in
            CLLocationCoordinate2D(latitude: loc.latitude, longitude: loc.longitude)
        }
        
        let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))

        let data = trackLine.geoJSONData(usingEncoding: String.Encoding.utf8.rawValue)
        let getJson = GeoJSON(objectString: String(data: data, encoding: .utf8)!)
       
        options.overlays.append(getJson)
1

There are 1 best solutions below

1
On BEST ANSWER

You need to use MGLPolylineFeature which allows to control feature attributes.

So instead of

let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))

it should look something like

let trackLine = MGLPolylineFeature(coordinates: coordinates!, count: UInt(coordinates!.count))
        
polyline.attributes.updateValue("red", forKey: "stroke")