如何从Mapbox GL JS中的样式层获取功能?

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

我是JavaScript的学习者并且遇到Mapbox GL JS的问题:我在Mapbox Studio中有一个样式,其中有one my layer — "locations"。我已将其添加为tileset。这一层有两个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);
    });

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

Here is关于GitHub的项目。

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

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

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

map.getSource('composite').vectorLayerIds

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

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

返回4个功能。

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