Javascript js API自动缩放到GeoJSON

Javascript js API自动缩放到GeoJSON,javascript,maps,mapbox,Javascript,Maps,Mapbox,我有一个简单的页面,其中有一个基于mapbox的地图,使用GeoJSON填充所需的行字符串。我正在寻找一种在每次加载地图时自动设置范围的方法。目前,我正在计算最大和最小lat/long,并以此方式设置视图,但在执行此操作时,我仍然需要定义缩放级别。我见过一些与设置边界相关的函数,但在仍然使用GeoJSON的情况下,我似乎无法实现这些功能。有什么建议吗 <script> L.mapbox.accessToken = '[removed]'; var geojs

我有一个简单的页面,其中有一个基于mapbox的地图,使用GeoJSON填充所需的行字符串。我正在寻找一种在每次加载地图时自动设置范围的方法。目前,我正在计算最大和最小lat/long,并以此方式设置视图,但在执行此操作时,我仍然需要定义缩放级别。我见过一些与设置边界相关的函数,但在仍然使用GeoJSON的情况下,我似乎无法实现这些功能。有什么建议吗

    <script>
    L.mapbox.accessToken = '[removed]';

    var geojson = [

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.116589,49.209164],[-123.117103,49.208972],[...removed for clarity...],[-123.111908,49.284047] ]
                },
                "properties": {
                    "title": "10 DOWNTOWN",
                    "stroke": "#0324a7",
                    "stroke-width": 5,
                    "stroke-opacity": 1
                }
            },

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.134782,49.203239],[-123.135147,49.203328],[-123.135894,49.203637],[-123.136782,49.203951],[-123.138169,49.204423],[-123.138759,49.20457],[-123.139587,49.204862],[-123.140005,49.205046],[-123.140243,49.205186],[-123.140395,49.205325],[-123.140532,49.205506],[-123.140631,49.205792],[-123.140586,49.20707],[-123.140534,49.208535],[-123.140463,49.210515],[-123.140448,49.210886],[-123.140432,49.211372],[-123.140401,49.212256],[-123.14037,49.213145],[-123.140369,49.213145],[-123.14036,49.213735],[-123.140355,49.213998],[-123.140776,49.214006],[-123.140789,49.213746] ]
                },
                "properties": {
                    "title": "10 GRANVILLE TO 63RD",
                    "stroke": "#0324a7",
                    "stroke-width": 5,
                    "stroke-opacity": 1
                }
            },

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.134782,49.203239],[-123.135147,49.203328],[...removed for clarity...],[-123.127194,49.277803],[-123.128135,49.278406] ]
                },
                "properties": {
                    "title": "10 TO DAVIE",
                    "stroke": "#0324a7",
                    "stroke-width": 5,
                    "stroke-opacity": 1
                }
            },

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.134782,49.203239],[-123.135147,49.203328],[...removed for clarity...],[-123.116191,49.281213],[-123.117871,49.280109],[-123.118797,49.280722],[-123.119815,49.281368] ]
                },
                "properties": {
                    "title": "10 TO ROBSON",
                    "stroke": "#0324a7",
                    "stroke-width": 5,
                    "stroke-opacity": 1
                }
            },

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.110919,49.284708],[-123.111908,49.284047],[-123.112864,49.283416],[...removed for clarity...],[-123.116589,49.209164] ]
                },
                "properties": {
                    "title": "10 GRANVILLE",
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                }
            },

                {
                "type": "Feature",
                "properties": {
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [    [-123.110919,49.284708],[-123.111908,49.284047],[...removed for clarity...],[-123.134852,49.202151] ]
                },
                "properties": {
                    "title": "10 TO MARPOLE",
                    "stroke": "#0324a7",
                    "stroke-width": 2,
                    "stroke-opacity": 1
                }
            }];

    var map = L.mapbox.map('map', 'mapbox.streets');
    map.setView([49.2437385, -123.1258535], 12);



    L.geoJson(geojson, { style: L.mapbox.simplestyle.style }).addTo(map);

</script>
提前谢谢

L.GeoJSON从L.FeatureGroup继承getBounds方法。您可以使用它获取图层的边界:

返回要素编组的LatLngBounds,该要素编组是根据其子要素的边界和坐标创建的

您可以将这些边界与L.mapbox.map L.map实例的fitBounds方法一起使用:

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


太棒了-非常有意义。谢谢下面是我的代码var geojsonlayer=L.geoJsongeojson,{style:L.mapbox.simplestyle.style}.addTomap;var bounds=geojsonlayer.getBounds;map.fitbundsbounds;
var geojsonLayer = L.geoJson(geojson, {
    style: L.mapbox.simplestyle.style
}).addTo(map);

var bounds = geojsonLayer.getBounds();
map.fitBounds(bounds);