缩放以适合 Mapbox 或 Leaflet 中的所有标记

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

如何设置视图以查看MapboxLeaflet中地图上的所有标记?就像 Google Maps API 所做的那样

bounds

例如:

var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < latlng.length; i++) {
  latlngbounds.extend(latlng[i]);
}
map.fitBounds(latlngbounds);
leaflet mapbox
12个回答
355
投票
var group = new L.featureGroup([marker1, marker2, marker3]);

map.fitBounds(group.getBounds());

请参阅文档了解更多信息。


30
投票

由于某些原因,“答案”对我不起作用。这就是我最终所做的:

////var group = new L.featureGroup(markerArray);//getting 'getBounds() not a function error.
////map.fitBounds(group.getBounds());
var bounds = L.latLngBounds(markerArray);
map.fitBounds(bounds);//works!

21
投票
var markerArray = [];
markerArray.push(L.marker([51.505, -0.09]));
...
var group = L.featureGroup(markerArray).addTo(map);
map.fitBounds(group.getBounds());

17
投票

Leaflet 还具有 LatLngBounds,甚至具有扩展功能,就像谷歌地图一样。

http://leafletjs.com/reference.html#latlngbounds

所以你可以简单地使用:

var latlngbounds = new L.latLngBounds();

其余部分完全相同相同。


11
投票

尽管 Mapbox 有更多功能,例如填充、动画和最大缩放,但我迟迟没有看到任何使用 Mapbox 的答案。


const coordinates = [<coordiantes>...]

// Create a 'LngLatBounds' with the first coordinate.
const bounds = new mapboxgl.LngLatBounds(
  coordinates[0],
  coordinates[0]
);
 
// Extend the 'LngLatBounds' to include every coordinate in the bounds result.
for (const coord of coordinates) {
  bounds.extend(coord);
}

// Note there are other options such as easeing animation and maxZoom
map.fitBounds(bounds, {
  padding: 20
});

演示


7
投票

对于传单,我正在使用

    map.setView(markersLayer.getBounds().getCenter());

6
投票

你有一个 L.Marker 数组:

let markers = [marker1, marker2, marker3]

let latlngs = markers.map(marker => marker.getLatLng())

let latlngBounds = L.latLngBounds(latlngs)

map.fitBounds(latlngBounds)
// OR with a smooth animation
// map.flyToBounds(latlngBounds)

4
投票

您还可以找到一个要素组内的所有要素或所有要素组, 看看它是如何工作的!

//Group1
m1=L.marker([7.11, -70.11]);
m2=L.marker([7.33, -70.33]);
m3=L.marker([7.55, -70.55]);
fg1=L.featureGroup([m1,m2,m3]);

//Group2
m4=L.marker([3.11, -75.11]);
m5=L.marker([3.33, -75.33]);
m6=L.marker([3.55, -75.55]);
fg2=L.featureGroup([m4,m5,m6]);

//BaseMap
baseLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var map = L.map('map', {
  center: [3, -70],
  zoom: 4,
  layers: [baseLayer, fg1, fg2]
});

//locate group 1
function LocateOne() {
    LocateAllFeatures(map, fg1);
}

function LocateAll() {
    LocateAllFeatures(map, [fg1,fg2]);
}

//Locate the features
function LocateAllFeatures(iobMap, iobFeatureGroup) {
		if(Array.isArray(iobFeatureGroup)){			
			var obBounds = L.latLngBounds();
			for (var i = 0; i < iobFeatureGroup.length; i++) {
				obBounds.extend(iobFeatureGroup[i].getBounds());
			}
			iobMap.fitBounds(obBounds);			
		} else {
			iobMap.fitBounds(iobFeatureGroup.getBounds());
		}
}
.mymap{
  height: 300px;
  width: 100%;
}
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<link href="https://unpkg.com/[email protected]/dist/leaflet.css" rel="stylesheet"/>

<div id="map" class="mymap"></div>
<button onclick="LocateOne()">locate group 1</button>
<button onclick="LocateAll()">locate All</button>


3
投票

为了仅适合可见标记,我有这个方法。

fitMapBounds() {
    // Get all visible Markers
    const visibleMarkers = [];
    this.map.eachLayer(function (layer) {
        if (layer instanceof L.Marker) {
            visibleMarkers.push(layer);
        }
    });

    // Ensure there's at least one visible Marker
    if (visibleMarkers.length > 0) {

        // Create bounds from first Marker then extend it with the rest
        const markersBounds = L.latLngBounds([visibleMarkers[0].getLatLng()]);
        visibleMarkers.forEach((marker) => {
            markersBounds.extend(marker.getLatLng());
        });

        // Fit the map with the visible markers bounds
        this.map.flyToBounds(markersBounds, {
            padding: L.point(36, 36), animate: true,
        });
    }
}

1
投票
//Create a point Layer (in this case a GeoJSON source)
let pointLayer = L.geoJSON(pointSource, {
    pointToLayerL pointToLayer,
    onEachFeature: onEachFeature
});
//Add the newly created layer to the mapObject
pointLayer.addTo(mapObject);

//Set a padding variable for the space around the bounds
let paddingCoef = (0.5);

//Call the fit bounds to adjust to the padded extent of points.
mapObject.fitBounds(pointLayer.getBounds().pad(paddingCoef));

0
投票

最好的方法是使用下一个代码

var group = new L.featureGroup([marker1, marker2, marker3]);

map.fitBounds(group.getBounds());

0
投票

在MapBox中你可以使用fitBound方法。

您可以使用 docs.mapbox

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