[英]How to limit vertically the extent of an OpenLayers map?
當我向北平移時,地圖消失了,我看到了HTML的白色背景。 我想垂直平移,地圖到達頂部(或底部)時要阻塞。
我曾嘗試設置邊界,但是此解決方案也會水平放置邊界。 我希望能夠向左或向右無限搖攝,但限制上下搖攝。
我該怎么辦?
謝謝
編輯1:我更願意不擴展OpenLayers核心。 我希望解決方案涵蓋所有平移,拖動,縮放情況。 我認為,一般的解決方案是可以的。 我聽說地圖初始化中有一個選項,該選項僅在垂直方向上限制范圍。 你知道嗎
我通過重寫OpenLayers.Control.PanZoom的buttonDown函數來實現此目的,該函數如下所示:
buttonDown: function (evt) {
if (!OpenLayers.Event.isLeftClick(evt)) {
return;
}
switch (this.action) {
case "panup":
this.map.pan(0, -this.getSlideFactor("h"));
break;
switch (this.action) {
case "pandown":
this.map.pan(0, this.getSlideFactor("h"));
break;
... etc
因此,您可以執行以下操作:
OpenLayes.Control.PanZoom.prototype.buttonDown = function (evt){
if (!OpenLayers.Event.isLeftClick(evt)) {
return;
}
//get map bounds
var mapBounds=map.getExtent();
switch (this.action) {
case "panup":
//add you code here to check extents
if (mapBounds.top < some_number){
this.map.pan(0, -this.getSlideFactor("h"));
break;
}
switch (this.action) {
case "pandown":
//add you code here to check extents
if (mapBounds.bottom > some_number){
this.map.pan(0, this.getSlideFactor("h"));
break;
}
//leave this as it was, as you don't care about left/right
case "panleft":
this.map.pan(-this.getSlideFactor("w"), 0);
break;
....
等等。請注意,您必須覆蓋其余的功能,因為要覆蓋它。 您要確保在加載OpenLayers.js之后添加此函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.