javascript - 带有轨道 fitbounds 的传单 featureGroup 无效
问题描述
有没有更好的办法???
我正在向地图添加几条轨道(使用Leaflet-gpx 插件),并希望将地图适合所有添加的轨道的边界。
通过使用 featureGroup 层来包含轨道,可以使用 fitBounds() 方法使用特征组。然而,featureGroup 没有加载事件来触发 fitBounds() 并且地图似乎也没有合适的触发器。
渲染后直接在地图上调用 fitBounds() 失败并出现“无效边界”错误。但是,将调用延迟大约一秒钟可以使其正确执行。
我认为正在发生的事情是加载组中的所有 gpx 图层需要时间,并且在完成之前,该组无法返回一组有效的边界。
因此,使用 setTimeout 来延迟 fitBounds() 可以工作,但会在地图重新渲染自身时导致一些烦人的闪烁。有没有更好的办法?
这有效:
// mapname has been created and tracks have already been loaded into their individual layers
var tracksLayer = L.featureGroup([track1,track2,track3]);
tracksLayer.addTo(mapname);
//this works, with more and complex tracks you might need a longer timeout
setTimeout(function(){ mapname.fitBounds(tracksLayer.getBounds()); }, 1000);
// this however does not work
// mapname.on('load', function() { mapname.fitBounds(tracksLayer.getBounds());});
//nor does this despite the fact that by this stage the map has been created and the featureGroup added
// mapname.fitBounds(tracksLayer.getBounds());
解决方案
推荐阅读
- graylog2 - 在 Graylog2 中,是否可以使用提取器创建一个以换行符显示的字段?
- r - R中的时间戳转换?
- php - 如何在 Laravel 中使用 Route::view 方法将 url 参数传递给视图?
- javascript - 在每个按钮单击时显示数组中的图像
- xcode - 使用 Xcode 首选项与 .gitignore 文件
- haskell - Simple examples to clarify on memoisation and strictness
- java - Firebase 数据库无法检索值
- python - 从 Anaconda 64 位安装切换到 32 位安装,但保持安装库
- c# - 为什么对象和集合初始值设定项的组合使用 Add 方法?
- scala - 无法更改 Spark foreach 函数体内的全局变量