[英]OpenLayers: lock map to the vector's extents
I'm learning how it is possible to create a library in JavaScript using OpenLayers.我正在学习如何使用 OpenLayers 在 JavaScript 中创建一个库。 Normally I use the code below to lock my map on the vector extent passed manually.
通常我使用下面的代码将我的 map 锁定在手动传递的矢量范围上。
var minX= 3.0025038498794938;
var minY= 47.7706339888675302;
var maxX= 5.1702255722362533;
var maxY= 49.5690401585264695;
var maxExtent = [minX, minY, maxX, maxY];
var boundary = ol.proj.transformExtent(maxExtent, 'EPSG:4326', 'EPSG:3857');
var center = ol.proj.transform([4.0863647111, 48.6698370737], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
extent: boundary,
center: center,
zoom: 9,
maxZoom: 18,
minZoom: 8,
});
map.setView(view);
My aim is use the same phylosophy but using the extent of a vector inside a specific function. I'm be able to load the map on a zoom on the vector extensions:我的目标是使用相同的哲学,但使用特定 function 内的矢量范围。我能够在矢量扩展的缩放上加载 map:
function getPolygonsExtent() {
vectorsLayer.getSource().once('change', function(evt) {
if (vectorsLayer.getSource().getState() === 'ready') {
if (vectorsLayer.getSource().getFeatures().length > 0) {
extent = vectorsLayer.getSource().getExtent();
options = {
size: map.getSize(),
padding: [0, 0, 0, 0],
}
map.getView().fit(extent, options);
}
}
});
};
But I've no idea how I can follow my aim.但我不知道如何才能实现我的目标。 I thought there was a function like
map.getView().setExtent(extent);
我以为有一个 function 像
map.getView().setExtent(extent);
, but is is not so. ,但事实并非如此。 Any suggestions?
有什么建议么?
There is no setExtent()
method so you will need to create a new view没有
setExtent()
方法,因此您需要创建一个新视图
map.getView().fit(extent, options);
var newView = new ol.View({
extent: extent,
showFullExtent: true,
center: map.getView().getCenter(),
zoom: map.getView().getZoom(),
maxZoom: 18,
minZoom: 8,
});
map.setView(newView);
The fit()
method fits the extent inside the map, but by default the extent option constrains the map inside the extent, use showFullExtent
for similar behaviour to fit()
. fit()
方法适合 map 内的范围,但默认情况下,范围选项将 map 限制在范围内,使用showFullExtent
以获得与fit()
类似的行为。 If your fit options include a duration
you will also need a callback
to update the view when it is finished inside of doing it in inline code.如果您的适合选项包括一个
duration
,您还需要一个callback
来更新视图,当它在内联代码中完成时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.