【问题标题】:Zoom to fit leaflet.js markers缩放以适合 leaflet.js 标记
【发布时间】:2015-05-23 02:00:43
【问题描述】:

我正在将我的网站从谷歌地图转换为传单/openstreetmap。在该网站上,搜索结果显示为地图上的标记。结果可以有一个标记,或 10 个,或 200 个。这一切都很好。但是当结果有多个标记时,我很难让它缩放/适合所有标记。相反,它只是放大了一个(可能是因为我将 map.setView 放大到 18!)。

在我的情况下,有没有使用 map.setView 的替代方法?我不想在所有情况下都将缩放设置为 18。但希望缩放只适合内容。我知道关于 SO 有一些类似的问题,但它们没有帮助,因为我不知道如何用不会对缩放进行硬编码的东西替换 map.setView。这是我的全部功能:

function showLocations(ids, lats, lons, contents) {
  locationIDs = ids;
  infoWindows = new Array();
  markers = new Array();

  map = new L.Map('map_canvas');
  var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
  var osm = new L.TileLayer(osmUrl, {minZoom: 1, maxZoom: 19, attribution: osmAttrib});

  for (i in ids) {

    var infoWindow = L.popup()
    .setContent(contents[i]);   

    map.setView(new L.LatLng(lats[i], lons[i]),18);
    map.addLayer(osm);
    L.marker([lats[i], lons[i]]).addTo(map)
    .bindPopup(infoWindow)
    .openPopup();

  }
}

用谷歌地图,我用这个:

markers.push(marker);
bounds.extend(latlng);

if (contents.length === 1) {
  map.setZoom(18);
  map.setCenter(new google.maps.LatLng(lats[0], lons[0]));
} else {
  map.fitBounds(bounds);
}

谢谢!

【问题讨论】:

    标签: leaflet openstreetmap fitbounds


    【解决方案1】:

    Leaflet 也有一个LatLngBounds 类,带有一个extend 方法,而地图有一个fitBounds 方法,因此您可以1:1 移植谷歌地图代码。

    还有几点:循环内部不用调用map.addLayer(osm),调用一次就够了;将var 添加到所有变量中是一种很好的做法;而for (i in ids) 是遍历数组的危险方式,最好调用ids.forEach

    function showLocations(ids, lats, lons, contents) {
      var infoWindows = new Array();
      var markers = new Array();
    
      var map = new L.Map('map_canvas');
      var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
      var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
      var osm = new L.TileLayer(osmUrl, {minZoom: 1, maxZoom: 19, attribution: osmAttrib});
      map.addLayer(osm);
    
      var bounds = new L.LatLngBounds();
    
      ids.forEach(function(i) {
        var infoWindow = L.popup().setContent(contents[i]);
    
        var marker = L.marker([lats[i], lons[i]])
          .addTo(map)
          .bindPopup(infoWindow)
          .openPopup();
    
        bounds.extend(marker.getLatLng());
      });
    
      map.fitBounds(bounds);
    }
    

    【讨论】:

    • 非常感谢,亚历克斯!我还没有插入它,但这看起来很棒。我很感激额外的提示。我在阅读教程/示例时有时会遇到的一个问题是它们并不总是显示完整的功能(考虑到每种情况都不同,这是有道理的),并且像 fitBounds 和 LatLngBounds 这样的东西最终无法与我的其他东西配合得很好.虽然我尝试过,但我还不够熟练,无法自己解决所有问题!
    • 你知道当我得到leaflet.js 错误时是什么意思吗?例如“TypeError: o.DomUtil.getPosition(...) is undefined”或“TypeError: this._startPos is undefined”或“TypeError: this._northEast is undefined”。我正在使用来自 cdn 的 Leaflet-0.7.3。
    • 设置一个例子来说明你的问题,否则无法猜测。
    • 当我使用你的例子时会发生这种情况。并且任何时候我使用 fitBounds 或获取 LatLng 或 LatLngBounds。我的猜测是它无法确定边界的边缘在哪里。可能相关:stackoverflow.com/questions/17277686/…
    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2013-05-26
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多