Javascript Mapbox queryRenderedFeatures已加载
我想在页面加载后使用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
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(){})中代码>作为要查询的图层