如何从geojson获取mapbox GL的'line-dasharray'值

问题描述 投票:0回答:1

我正在尝试通过['get','line-dasharray']获取值,但出现错误

这是我的代码

var data = {
          'type': 'FeatureCollection',
          'features': [
 {
              'type': 'Feature',
              'geometry': {
                'type': 'LineString',
                'coordinates': []
              },
              'properties': {
                'route_id': 300,
                'route_url': 'http://test.com/file.gpx',
                'line-color': '#426d7e',
                'line-width':     3,
                'line-opacity':   1,
                'line-dasharray': [5, 2]
              },
            },
]
    }

// some code



              map.addSource('locations-rote', {
                type: 'geojson',
                data: data,
                generateId: true
              });

map.addLayer({
                  'id': 'route-coordinates',
                  'type': 'line',
                  'source': 'locations-rote',
                  'layout': {
                    'line-join': 'round',
                    'line-cap': 'round'
                  },
                  'paint': {
                    'line-color': ['get', 'line-color'],
                    'line-width': ['get', 'line-width'],
                    'line-opacity': ['get', 'line-opacity'],
                    'line-dasharray': ['get', 'line-dasharray']
                  }
                });

我尝试使用数组表达式-https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#types-array,但也会收到错误。告诉我如何解决问题。

javascript mapbox mapbox-gl-js mapbox-gl
1个回答
0
投票

尽管您没有指定得到的错误是什么,但是很可能出现类似以下内容:

Error: layers.route-coordinates.paint.line-dasharray: data expressions not supported

line-dasharray文档here的SDK支持所指出的,这是因为尚不支持数据驱动的样式(即,使用expression来确定属性值,以及从源数据派生的信息) GL JS上的line-dasharray属性。

如果要根据不同的属性对虚线的样式进行设置,可以通过应用filters来创建一些引用此源数据的不同图层。

© www.soinside.com 2019 - 2024. All rights reserved.