javascript - Mapbox queryRenderedFeatures on load
问题描述
我想在页面加载后使用 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);
});
解决方案
来自https://github.com/mapbox/mapbox-gl-js/issues/4222#issuecomment-279446075:
您可以检查map.loaded()
(https://www.mapbox.com/mapbox-gl-js/api/#map#loaded)以确定地图是否已加载以及查询功能是否安全。
示例代码参见 GitHub 上的链接问题评论。
推荐阅读
- python-3.x - lru_cache 为所有类实例保存
- elasticsearch - Elasticsearch 在脚本字段中访问 params['_source'] 时返回 null_pointer_exception
- php - MySqlBuilder::defaultStringLength()
- rpm - 从 RPM DB、BerkeleyDB 获取已安装的软件包
- azure-devops - 部署到 Visual Studio App Center 的二进制文件路径
- sublimetext3 - SublimeText 不打开多个文件
- php - 使用带有 php 页面的 Android 在 MySQL 中添加数据
- python - 在 Pandas 或 SQL 中保留包含非英语单词的行
- r - 如何将 ggplot2 for R 安装到自定义 lib 目录
- javascript - 如何设置 VSCode 调试器以在 Angular 6 中使用 Protractor E2E 测试