且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Mapbox GL JS中的样式层获取要素?

更新时间:2023-01-01 22:12:54

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

Your problem is that your map, like all maps created in Mapbox Studio by default, uses auto-compositing. You don't actually have a source called morganvolter.cj77n1jkq1ale33jw0g9haxc0-2haga, you have a source called composite with many sub layers.

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

You can find the list of layers like this:

map.getSource('composite').vectorLayerIds

这表明您有一个名为 akkerman 的矢量图层.(" locations "是您的 style 层的名称,而不是您的 source 层的名称).因此,您的查询应为:

Which reveals you have a vector layer called akkerman. ("locations" is the name of your style layer, not your source layer). Hence your query should be:

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

将返回4个特征.