Mapbox GL JS getBounds undefined

问题描述 投票:0回答:1

我一直在尝试使用Mapbox GL JS绘制多个标记,并在这些标记上设置地图fitBounds。标记在地图上的绘制很好,我只是在fitBounds部分遇到问题。

我不断收到此控制台错误:

Uncaught TypeError: Cannot read property 'lng' of undefined
lng_lat_bounds.js:154 

这是我的JS来绘制标记。

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v10',
    center: [-96.2, 37.8],
    zoom: 9
});

// add markers to map
var bounds = new mapboxgl.LngLatBounds();
for ( var i=0; i < markersArray.length; ++i ){

    markersArray[i].features.forEach(function(marker) {

        // create a HTML element for each feature
        var el = document.createElement('div');
        el.className = 'marker';

        // make a marker for each feature and add it to the map
        new mapboxgl.Marker(el)
            .setLngLat(marker.geometry.coordinates)
            .setPopup(new mapboxgl.Popup({offset: 25}) // add popups
                .setHTML('<h3>' + marker.properties.title + '</h3><p>' + marker.properties.description + '</p>'))
            .addTo(map);
    });
}
map.fitBounds(bounds);
mapbox mapbox-gl-js mapbox-gl
1个回答
2
投票

您实际上想为每个标记lng,lat加上bounds.extend来扩展边界。

© www.soinside.com 2019 - 2024. All rights reserved.