在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提示窗口。
效果如图哦