我有一个GeoJSON对象,我希望在不将其添加到地图的情况下获取其边界,类似于L.polygon().getBounds()
所能获得的内容。
有没有办法轻松做到这一点?
也许将L.geoJSON
对象直接转换为L.polygon
?
答案 0 :(得分:1)
通过L.geoJSON
工厂解析GeoJSON对象以便构建Leaflet GeoJSON图层组(扩展要素组),您可以让Leaflet使用该组计算其子图层(要素)的边界getBound()
方法,无需将组添加到地图中。
var map = L.map('map').setView([48.86, 2.35], 11);
var geojsondata = {"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [2.34, 48.86]}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [2.36, 48.86]}}]};
var geojsongroup = L.geoJSON(geojsondata);
//geojsongroup.addTo(map);
alert(geojsongroup.getBounds().toBBoxString());
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet-src.js"></script>
<div id="map" style="height: 200px"></div>