如何将地图拟合到传单中的图块层边界

时间:2013-10-21 10:40:29

标签: javascript leaflet


我有一张传单地图。与第一缩放级别(256px X 256px)的图块相比,地图容器非常大。我想获得tile的边界并使地图边界适合它。我可以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));
    };

2 个答案:

答案 0 :(得分:4)

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

map.fitBounds(featureGroup.getBounds());

功能组类似于layerGroup,但也有.getBounds()方法可用于设置您的拟合。见the docs.

答案 1 :(得分:1)

可能不是最好的解决方案,但至少工作: 添加其他图层并使其不可见。放大到这一个。优点是,您还可以添加 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());