移动视角适应所有点
chatgpt
var map = new ol.Map({
target: 'map',
layers: [
],
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var vectorSource = new ol.source.Vector();
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
var randomPoints = generateRandomPoints(10);
randomPoints.forEach(function(point) {
var feature = new ol.Feature(new ol.geom.Point(ol.proj.fromLonLat(point)));
var pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: 'red' }),
stroke: new ol.style.Stroke({ color: 'white', width: 2 })
})
});
feature.setStyle(pointStyle);
vectorSource.addFeature(feature);
});
var extent = vectorSource.getExtent();
map.getView().fit(extent, { padding: [20, 20, 20, 20], duration: 1000 });
function generateRandomPoints(numPoints) {
var points = [];
for (var i = 0; i < numPoints; i++) {
points.push([Math.random() * 360 - 180, Math.random() * 180 - 90]);
}
return points;
}