2011-01-18 65 views
12

下有可選擇的圖層我有一個OpenLayers地圖,其中包含一個柵格基礎圖層,一個矢量圖層和一個標記圖層。它們以正確的順序顯示正確,並且矢量頂部的標記很好。強制OpenLayers標記圖層頂部繪製,並在

但是,當我添加SelectFeature控件並將其指向矢量圖層時,它突然被繪製在標記圖層上方,儘管所有努力都會提高標記圖層或設置Z指數。看起來SelectFeature控件會覆蓋所有繪圖順序設置。這是設計,還是我可以克服這個莫名其妙?

的層定義:

var baselayer = new OpenLayers.Layer.WMS('Norden', 
'http://{myarcgisserver}/ArcGIS/services/mylayer/MapServer/WMSServer', { 
    layers :'1,2', 
    transparent :false, 
    width :'auto', 
    height :'auto', 
    filter :null 
}, { 
    isBaseLayer: true, 
    singleTile :true, 
    ratio :1, 
    alpha :false, 
    transitionEffect :'resize' 
}); 

var vectorLayer = new OpenLayers.Layer.Vector("Work orders", { 
    projection: new OpenLayers.Projection("EPSG:2400"), 
    strategies: [new OpenLayers.Strategy.Fixed(), refresh], 
    protocol: new OpenLayers.Protocol.HTTP({ 
     url: "/WorkOrder/WorkOrders.ashx?output=geojson", 
     format: new OpenLayers.Format.GeoJSON() 
    }) 
}); 

var markerlayer = new OpenLayers.Layer.Markers("Markers", { 
    projection: new OpenLayers.Projection("EPSG:2400"), 
    displayInLayerSwitcher: false 
} 
); 

控制定義:

var selectctrl = new OpenLayers.Control.SelectFeature(
    vectorLayer, 
    { 
     clickout: true, 
     toggle: false, 
     multiple: false, 
     hover: false, 
     toggleKey: "ctrlKey", // ctrl key removes from selection 
     multipleKey: "shiftKey", // shift key adds to selection 
     box: false 
    } 
); 

激活:(沒有這一點,這些層在正確的順序繪製)

map.addControl(selectctrl); 

selectctrl.activate(); 

編輯: 實測這在OpenLayers.Handler.Feature中,「moveLayerToTop」的感覺就像罪魁禍首......會試圖克服它,但如果有人知道這是不可能的,請讓我知道!

/** 
* Method: activate 
* Turn on the handler. Returns false if the handler was already active. 
* 
* Returns: 
* {Boolean} 
*/ 
activate: function() { 
    var activated = false; 
    if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
     this.moveLayerToTop(); 
     this.map.events.on({ 
      "removelayer": this.handleMapEvents, 
      "changelayer": this.handleMapEvents, 
      scope: this 
     }); 
     activated = true; 
    } 
    return activated; 
}, 

回答

7

答案 - 如果可以稱之爲位於我上面提到的激活函數中。我試圖覆蓋它,並刪除了對moveLayerToTop的調用,它的作用就像一個魅力。

編輯: 我最終將此代碼添加到OL代碼庫之外的js文件,覆蓋處理程序激活功能。這是因爲我會在OpenLayers代碼庫的更新中失去更改。

OpenLayers.Handler.Feature.prototype.activate = function() { 
    var activated = false; 
    if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
     //this.moveLayerToTop(); 
     this.map.events.on({ 
      "removelayer": this.handleMapEvents, 
      "changelayer": this.handleMapEvents, 
      scope: this 
     }); 
     activated = true; 
    } 
    return activated; 
}; 
+0

你先生是某種天才! – Johnny 2011-03-11 10:53:40

+0

@Johnny:哈哈,謝謝!你讓我今天一整天都感覺很好! :-) – 2011-03-11 14:29:17

2

我發現這個時,我有同樣的問題,試圖讓多個層對鼠標事件作出反應。

解決方案,以防其他人發現這個線程更簡單。

SelectFeature控件需要一個Vector圖層的數組,並且如果需要對鼠標事件(懸停和單擊)作出反應的所有對象位於該數組中,則它們都可以工作,而不僅僅是移動到頂部的那個。

該文檔建議不要使用標記圖層。雖然我的解決方案圍繞着PostGIS幾何字段,並且適用於在矢量圖層中呈現POINT數據,但使用標記的任何事情都可以通過這種方式完成,並且根據OpenLayers的說法,應該這樣做。

所以,認可的解決方案,以這個線程可以多使用矢量圖層的標誌物,做這樣的事情簡單化:

this.carSelect = new OpenLayers.Control.SelectFeature(
    [vectorsLayer, markersLayer], 
    { 
     'hover':true, 
     'callbacks': { 
      blah blah blah 
    } 
}); 

這將註冊在兩層適當的活動,讓他們都住。

我希望這可以幫助任何人在這個問題上磕磕絆絆。

正如其他地方所說的,使用OpenLayers並不難,找到正確的方式來處理它。