mapbox / mapbox-gl-draw

Draw tools for mapbox-gl-js
https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-draw/
ISC License
942 stars 590 forks source link

Unable to use line-gradient paints styles for lines #858

Open karlTGA opened 5 years ago

karlTGA commented 5 years ago

mapbox-gl-js version: 0.52 mapbox-gl-draw version: 1.1.1

Steps to Trigger Behavior

  1. Set a line-gradient value in a paint object of a line style.
    {
    id     : 'gl-draw-line-static-gradient',
    type   : 'line',
    filter : ['all', ['==', '$type', 'LineString'], ['==', 'mode', 'static']],
    paint  : {
      'line-color'   : 'red',
      'line-width'   : 10,
      'line-gradient': ['interpolate', ['linear'], ['line-progress'], 0, 'blue', 0.1, 'royalblue', 0.3, 'cyan', 0.5, 'lime', 0.7, 'yellow', 1, 'red'],
    },
    layout: {
      'line-cap': 'round',
      'line-join': 'round',
    },
    },

    Expected Behavior

Get a nice line with a gradient from green to red.

Actual Behavior

Get an error message: Error: layers.gl-draw-line-static-gradient.cold: layer "gl-draw-line-static-gradient.cold" specifies a line-gradient, which requires a GeoJSON source with lineMetrics enabled. at Object.Jr [as emitValidationErrors] (mapbox-gl.js:29) at De (mapbox-gl.js:33) at i._validate (mapbox-gl.js:33) at i.addLayer (mapbox-gl.js:33) at r.addLayer (mapbox-gl.js:33) at ctx.options.styles.forEach.style (setup.js:102) at Array.forEach () at Object.addLayers (setup.js:101) at r.connect (setup.js:35) at r.ht.fire (mapbox-gl.js:29)

andrewharvey commented 5 years ago

When the GeoJSON source is added to the map at https://github.com/mapbox/mapbox-gl-draw/blob/5b2280817c901d93b05c937a7bdb2efb8afefe0c/src/setup.js#L84-L99 you need to use 'lineMetrics: true'.

Mapbox GL Draw could probably provide an option for this so you can easily enable it.

karlTGA commented 5 years ago

Aha ok. Maybe you can add an optional parameter to the add function and give the options to mapbox through.
add(geojson: Object, options?: Object) So every special option is useable.