Search code examples
iosswiftswift4mapboxmglmapview

MGLPolyLine color change does not work - Swift 4, Mapbox


When I try to change the color of my MGLPolyline the app crashes and prints this fatal error (it also takes me :

Assertion failed: (r_ <= 1.0f), function Color, file /Users/distiller/project/include/mbgl/util/color.hpp, line 18.

I am using Mapbox. This is the code I'm using to change the color:

//Colors:
let orangeColor = UIColor(red: 253, green: 70, blue: 2, alpha: 100)
let purpleColor = UIColor(red: 101, green: 0, blue: 141, alpha: 100)
let redColor = UIColor(red: 254, green: 0, blue: 0, alpha: 100)
let pinkPurpColor = UIColor(red: 193, green: 6, blue: 245, alpha: 100)
let pinkColor2 = UIColor(red: 254, green: 0, blue: 131, alpha: 100)

func mapView(_ mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {

    let arrayOfColors = [pinkColor2, pinkPurpColor, redColor, purpleColor, orangeColor]

    let randomIndex = Int(arc4random_uniform(UInt32(arrayOfColors.count)))

    return arrayOfColors[randomIndex]
}

Solution

  • value < 1 and division 255.

    Swift 4.2

    can you try this code

    let orangeColor = UIColor(red: 253/255, green: 70/255, blue: 2/255, alpha: 1)