繁体   English   中英

如何从 Mapbox GL JS 中的样式层获取特征?

[英]How get features from a style's layer in Mapbox GL JS?

我是 JavaScript 的学习者,并且在使用 Mapbox GL JS 时遇到了问题:我在 Mapbox Studio 中有一个样式,其中有一个我的图层 - "locations" 我已将其添加为图块集。 该层中有两个 GeoJSON 点,但我无法在 GL JS 中获取它们。 我发现我应该使用方法querySourceFeatures(sourceID, [parameters]) ,但我在正确填充其参数时遇到问题。 我写:

var allFeatures = map.querySourceFeatures('_id-of-my-tyleset_', {
  'sourceLayer': 'locations'
});

..它不起作用。

更有趣的是,在后面的代码中,我将这一层与方法queryRenderedFeatures ,这没问题:

map.on('click', function(e) {
      var features = map.queryRenderedFeatures(e.point, {
        layers: ['locations']
      });

      if (!features.length) {
        return;
      }
      var feature = features[0];
      flyToPoint(feature);
      var popup = new mapboxgl.Popup({
          offset: [0, -15]
        })
        .setLngLat(feature.geometry.coordinates)
        .setHTML('<h3>' + feature.properties.name + '</h3>' + '<p>' +
          feature.properties.description + '</p>')
        .addTo(map);
    });

我已经阅读了很多关于在地图上添加图层的内容,并且知道答案很简单,但我无法实现解决方案,因此请提供帮助:)

这是GitHub 上的项目。

您的问题是您的地图与默认情况下在 Mapbox Studio 中创建的所有地图一样,使用自动合成。 你实际上并没有一个所谓的源morganvolter.cj77n1jkq1ale33jw0g9haxc0-2haga ,您有一个名为源composite有许多子层。

您可以找到这样的图层列表:

map.getSource('composite').vectorLayerIds

这表明您有一个名为akkerman的矢量图层。 (“ locations ”是您的样式层的名称,而不是您的层)。 因此,您的查询应该是:

map.querySourceFeatures('composite', {
  'sourceLayer': 'akkerman'
});

返回 4 个特征。

关于Mapbox get features after filterMapbox get features before filter有很多问题。 我可以看到有很多帖子散落着,但似乎没有一个有FULL DETAILED解决方案。 我花了一些时间将两个解决方案放在一个函数下,在 jsbin 中试试这个。 这是给有兴趣的人:

function buildRenderedFeatures(map) {
  // get source from a layer, `mapLayerId` == your layer id in Mapbox Studio    
  var compositeSource = map.getLayer(mapLayerId.toString()).source;
  //console.log(map.getSource(compositeSource).vectorLayers);
  var compositeVectorLayerLength = map.getSource(compositeSource).vectorLayers.length - 1;
  //console.log(compositeVectorLayerLength);
  // sourceId === tileset id which is known as vector layer id
  var sourceId = map.getSource(compositeSource).vectorLayers[compositeVectorLayerLength].id;
  //console.log(sourceId);
  
  // get all applied filters if any, this will return an array    
  var appliedFilters = map.getFilter(mapLayerId.toString());
  //console.log(appliedFilters);
   
  // if you want to get all features with/without any filters
  // remember if no filters applied it will show all features
  // so having `filter` does not harm at all 
  //resultFeatures = map.querySourceFeatures(compositeSource, {sourceLayer: sourceId, filter: appliedFilters});
  var resultFeatures = null;

  // this fixes issues: queryRenderedFeatures getting previous features
  // a timeout helps to get the updated features after filter is applied
  // Mapbox documentation doesn't talk about this! 
  setTimeout(function() {
    resultFeatures = map.queryRenderedFeatures({layers: [mapLayerId.toString()]});
    //console.log(resultFeatures);
    }, 500);
  }

然后您调用该函数,例如: buildRenderedFeatures(map)传递您在创建 Mapbox 地图时已经拥有的map对象。

然后,您将拥有resultFeatures将返回一个可以使用for...in迭代的对象。 您可以测试我注释掉的querySourceFeatures()代码,但如果有人需要它,则可以使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM