首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

请教openlayers中,怎么给point、line添加单击事件。多谢

2013-10-29 
请问openlayers中,如何给point、line添加单击事件。谢谢。我写的代码如下: var renderer OpenLayers.Util.g

请问openlayers中,如何给point、line添加单击事件。谢谢。
我写的代码如下:


 var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
            renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;

var vectors = new OpenLayers.Layer.Vector("Vector Layer Test", {//创建一个新的vector layer
                renderers: renderer
            });
vectors.events.on({
                'featureselected': function(feature) {
                    document.getElementById('counter').innerHTML = this.selectedFeatures.length;
                },
                'featureunselected': function(feature) {
                   document.getElementById('counter').innerHTML = this.selectedFeatures.length;
                }

            });
map.addLayers([vectors]);
 drawControls = {
                point: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Point
                ),

                line: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Path
                ),
                polygon: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Polygon
                ),
                select: new OpenLayers.Control.SelectFeature(
                    vectors,
                    {
                        clickout: false, toggle: false,
                        multiple: false, hover: false,
                        toggleKey: "ctrlKey", // ctrl key removes from selection
                        multipleKey: "shiftKey", // shift key adds to selection
                        box: true,

                    }
                ),
                selecthover: new OpenLayers.Control.SelectFeature(
                    vectors,
                    {
                        multiple: false, hover: true,
                        toggleKey: "ctrlKey", // ctrl key removes from selection
                        multipleKey: "shiftKey" // shift key adds to selection
                    }
                )



            };
           
            for(var key in drawControls) {
                map.addControl(drawControls[key]);
            }

 alert("drawControls:"+drawControls.line);

 vectors.events.register("click", vectors, function(){
alert("单击事件!");
if(drawControls.point.id){
alert("单击point!");
}else if(drawControls.line){
alert("单击line!");
}
else if(drawControls.polygon){
alert("单击polygon!");
}
 });
 vectors.events.register("dblclick", vectors, function(feature){
alert("清除");
vectors.removeFeatures(feature)
vectors.removeAllFeatures()
});




画点、线、面都是可以的,我想实现的功能就是画点的时候,就弹出 alert("单击point!");
线的时候就alert("单击line!");,面的时候就alert("单击polygon!");
请问怎么弄呢,
我通过firedebug 可以看到 drawControls 里面有line point等
但是咋取值呢,咋区分呢,谢谢。
[解决办法]
参考:

  OpenLayers-Vector图层Feature选择工具
[解决办法]
参考:

   点击地图div上的按钮实现对地图数据的操作:查询点、添加点、删除点 

热点排行