当前位置: 代码迷 >> GIS >> 请教openlayers中,怎么给point、line添加单击事件。多谢
  详细解决方案

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

热度:254   发布时间:2016-05-05 06:17:37.0
请问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
                    }
                )
  相关解决方案