-1

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Yosi199
  • 1,745
  • 4
  • 22
  • 47
  • I'm using MapBox not MapKit – Yosi199 Nov 01 '20 at 06:28
  • https://stackoverflow.com/questions/54566855/how-to-change-the-color-of-a-mglpolyline? – Anatolii Suhanov Nov 01 '20 at 10:49
  • Thanks @AnatolySukhanov but this is not the case - what you linked is talking about setting it on a live map which works fine for me. I'm talking about taking a snapshot of the map to create an image. It draws the route in black color and I dont know how to change it – Yosi199 Nov 01 '20 at 13:07
  • Sorry, I'm new to this and it took me some time to figure it out. See my answer. – Anatolii Suhanov Nov 03 '20 at 22:16

1 Answers1

1

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")
Anatolii Suhanov
  • 2,524
  • 1
  • 12
  • 14
  • Wow @Anatoly Sukhanov thank you so much it worked perfectly! I honestly didn't think I'd find an answer for this here - You are great buddy! – Yosi199 Nov 05 '20 at 18:54