leaflet如何根据多个点设置View

时间:2017-11-16 07:32:33

标签: leaflet

setView()函数接受中心坐标和。我从Google Place API中获取了大量积分,我想设置视图以在视图中包含这些点。

我已经计算了一个合适的边界框(int lat和lng),但不知道如何设置视图与lat和lng对齐,因为setView()只接受中心坐标和缩放,似乎是测量缩放像素

1 个答案:

答案 0 :(得分:2)

使用map.fitBounds()

  

设置一个地图视图,其中包含可能具有最大缩放级别的给定地理边界。

此外,您可以使用Feature Group然后group.getBounds()自动计算POI的相应边界框:

  

返回要素组的LatLngBounds(根据其子项的边界和坐标创建)。



var map = L.map("map").setView([48.85, 2.35], 10);

var markers = [
  L.marker([48.84, 2.34]),
  L.marker([48.85, 2.35]),
  L.marker([48.86, 2.36])
];

var group = L.featureGroup(markers).addTo(map);

setTimeout(function () {
  map.fitBounds(group.getBounds());
}, 1000);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
&#13;
<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>
&#13;
&#13;
&#13;

相关问题