0

I am trying to make an MKPolyline for a SwiftUI map where it shows a persons location for a day and I want a gradient changing from blue to green from the first point in their location to the last point in blue. I have this code

renderer.strokeColor = NSGradient(colors: [NSColor.blue, NSColor.green])

I have also tried

renderer.strokeColor = NSColor(NSGradient(colors: [NSColor.blue, NSColor.green]))

and

renderer.strokeColor = NSColor(Color(Gradient(colors: [Color.blue, Color.green])))

but these all return errors about turning Gradients into colors. Thanks!

WilliamD47
  • 129
  • 6
  • 1
    You cannot convert `NSColor` to `NSGradient` and vice versa. The type of `strokeColor` is `NSColor`. It's not a flexible SwiftUI `ShapeStyle`. To draw a gradient see https://stackoverflow.com/questions/5682688/gradient-polyline-with-mapkit-ios – vadian Sep 28 '22 at 17:11

1 Answers1

0

Thanks to @vadian I did this

if let routePolyline = overlay as? MKPolyline {
  let renderer = MKGradientPolylineRenderer(polyline: routePolyline)
    renderer.setColors([NSColor.blue, NSColor.green], locations: [])
  renderer.lineWidth = 2
  return renderer
}
WilliamD47
  • 129
  • 6