Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mapbox queryRenderedFeatures已加载_Javascript_Mapbox_Mapbox Gl Js - Fatal编程技术网

Javascript Mapbox queryRenderedFeatures已加载

Javascript Mapbox queryRenderedFeatures已加载,javascript,mapbox,mapbox-gl-js,Javascript,Mapbox,Mapbox Gl Js,我想在页面加载后使用queryRenderedFeatures来填充列表,但在加载层之前,它似乎一直处于激活状态。我在下面的控制台中收到错误消息: The layer 'Points' does not exist in the map's style and cannot be queried for features. 加载要素后,如何查询图层?我试着按照这些答案中的建议去做,但结果总是空的 这就是我现在拥有的 map.on('load', function() { map.add

我想在页面加载后使用queryRenderedFeatures来填充列表,但在加载层之前,它似乎一直处于激活状态。我在下面的控制台中收到错误消息:

The layer 'Points' does not exist in the map's style and cannot be queried for features.
加载要素后,如何查询图层?我试着按照这些答案中的建议去做,但结果总是空的

这就是我现在拥有的

map.on('load', function() {
  map.addLayer({
      'id': 'Points',
      'type': 'circle',
      'source': 'Points-45d56v',
      'source-layer': 'Points-45d56v',
      'layout': {
          'visibility': 'visible',
      },
      'paint': {
        'circle-radius': 6,
        'circle-color': 'red'
      }
  });
});

$(document).ready(function(){
  var features = map.queryRenderedFeatures({layers:['Points']});
  console.log(features);
});
发件人:

您可以检查
map.loaded()
()以确定地图是否已加载,以及查询要素是否安全


例如,代码请参见GitHub上的链接问题注释。

我在上一个答案中从GitHub链接中获得了以下代码,这对我很有用:

map.addLayer(...) // make your change
map.on('render', afterChangeComplete); // warning: this fires many times per second!

function afterChangeComplete () {
  if (!map.loaded()) { return } // still not loaded; bail out.

  // now that the map is loaded, it's safe to query the features:
  map.queryRenderedFeatures(...);

  map.off('render', afterChangeComplete); // remove this handler now that we're done.
}
确保将它放在相同的
map.on('load',function(){})中作为要查询的图层