【问题标题】:How to fit map to tile layer bounds in leaflet如何使地图适合传单中的平铺层边界
【发布时间】:2016-12-08 23:21:34
【问题描述】:


我有一张传单地图。与第一个缩放级别(256px X 256px)的图块相比,地图容器非常大。我想获得瓦片的边界并将地图边界拟合到它。我可以使用多个标记fitbound()。我采用了 here 的代码。我也尝试以相同的想法实现适合瓷砖。不幸的是,它似乎不起作用。这是我的代码

     var fitToBounds = function () {
         //getting bound of tile 256X256 pixel
        var maxLat = map.unproject(new L.Point(0,0)).lat; 
        var maxLat = map.unproject(new L.Point(0,256)).lat;
        var maxLng = map.unproject(new L.Point(256,0)).lng;
        var minLng = map.unproject(new L.Point(256,256)).lng;
        var southWest = new L.LatLng(minLat, minLng);
        var northEast = new L.LatLng(maxLat, maxLng);
        map.fitBounds(new L.LatLngBounds(southWest, northEast));
    };

【问题讨论】:

  • 你有两个变量叫做 maxLat... 一个应该是 minLat

标签: javascript leaflet


【解决方案1】:

将您的标记分组到 L.mapbox.featureGroup() 中,然后在您的 map 上使用

map.fitBounds(featureGroup.getBounds());

特征组类似于layerGroup,但也有一个.getBounds() 方法,可用于设置您的适合度。见the docs.

【讨论】:

  • 他没有说任何关于标记的事情。他有瓦片层,而瓦片层没有getBounds方法。您对标记分组的想法没有任何帮助。
【解决方案2】:

也许不是最好的解决方案,但至少可以: 添加另一个图层并使其不可见。放大到这个。优点是,您还可以添加 onEachFeature

     var pointsWithBounds=[]
     _.each(layerWithoutBounds, function (element) {
                pointsWithBounds.push(
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "CircleMarker",
                            "coordinates": [element.lon, element.lat]
                     });
      });
      var layerWithBounds = L.geoJSON(pointsWithBounds, {
                style: {
                          fillColor: 'transparent',
                          color: 'transparent'
                       }
            });
      layerWithBounds.addTo(map)
      map.setView(layerWithBounds.getBounds().getCenter());
      map.fitBounds(layerWithBounds.getBounds());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2015-08-12
    • 2021-12-09
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2017-10-13
    相关资源
    最近更新 更多