在使用GeoJSON将数据引入featureLayer的项目中,我无法使用fitBounds命令。
这一点:
map.fitBounds(pointsFeatureGroup.getBounds()); // Uncaught Error: Bounds are not valid. at e.fitBounds (leaflet.js:5)还有这个:
var bounds = L.latLngBounds(pointsFeatureGroup); // Uncaught Error: Bounds are not valid. at e.fitBounds
map.fitBounds(bounds);不工作
我的简化代码在这里:https://github.com/DPB61/leafletjs_problem2
如果我只是在普通图层中单独引入标记,那么像这样的命令就可以工作了:
map.fitBounds(PointsLayer.getBounds());但这引发了其他功能的其他问题,这些功能需要一个特征层。
我做错了什么吗?
发布于 2017-07-08 04:19:11
根本原因是一个典型的异步问题。
请参阅How do I return the response from an asynchronous call?
在调用map.fitBounds(vesselsFeatureGroup.getBounds())时,vesselsFeatureGroup仍然是空的(没有子层),因此Leaflet无法计算边界。
只需在异步任务($.getJSON)的回调中调用getBounds和fitBounds
https://stackoverflow.com/questions/44880792
复制相似问题