在map對象上注冊點擊方法監聽, 這里我用的是wms,當然你也可以查詢wfs
map.events.register('click', map, function (e) {
var params = {
REQUEST: "GetFeatureInfo",
EXCEPTIONS: "application/vnd.ogc.se_xml",
BBOX: map.getExtent().toBBOX(),
SERVICE: "WMS",
VERSION: "1.1.1",
X: e.xy.x,
Y: e.xy.y,
// INFO_FORMAT: 'text/html', 這里必須注意啦 查詢結果會返回不同的格式滴,根據自己的需呀要做不同設置哦,text/html或者application/vnd.ogc.gml
INFO_FORMAT: 'application/vnd.ogc.gml',
QUERY_LAYERS: map.layers[1].params.LAYERS,
FEATURE_COUNT: 50,
Layers: 'mine:locatorinfo_'+MineCode,
WIDTH: map.size.w,
HEIGHT: map.size.h,
format: format,
styles: map.layers[1].params.STYLES,
srs: map.layers[1].params.SRS};
// alert(map.layers[1].params.LAYERS);
OpenLayers.loadURL("http://172.16.0.81:8080/geoserver/mine/wms", params, this, setHTML, failhttml);
tpoint = map.getLonLatFromPixel(e.xy),
OpenLayers.Event.stop(e);
});
查詢完畢且成功回調方法是:setHTML 【加載查詢失敗的方法時failhtml】
var popup1;
function setHTML(response){
// alert(response.responseText);
//獲取要素結果集
var g = new OpenLayers.Format.GML();
var features = g.read(response.responseText);
var tempstr = '';
var nfid = '';
var nm = '';
//遍歷要素集
for(var feat=0;feat<features.length;feat++){
nm = features[feat].attributes.equipmentid; //獲取屬性的時候
nfid = features[feat].fid; //fid的獲取比較特殊哦,不是按照屬性獲取方式的,可以參考geoserver發布的服務就能看出那些時屬性設置
tempstr += nfid;
}
if(popup1!=null){
map.removePopup(popup1);
}
popup1 = new OpenLayers.Popup.FramedCloud("chicken",
tpoint,
new OpenLayers.Size(200,200),
tempstr,
null,
true);
popup1.autoSize=true;
map.addPopup(popup1);
};
執行邏輯是這樣滴,點擊圖層,查詢結果彈出tip提示窗口。
效果如圖哦