openlayer右键菜单_让OpenLayers的SelectFeature控件支持鼠标右键事件
OpenLayers上的一个Feature对象单击出现一个气泡很容易实现,但是右键出现点菜单什么的就不容易了,关键在于SelectFeature控件不支持右键事件,所以我就改之。。
修改的源文件是基于OpenLayers2.7+changeset9116
changeset9116修复了SelectFeature不能选取两个以上图层的bug,具体参见http://trac.openlayers.org/changeset/9116
所以有些源代码和官网上下的OpenLayers2.7有点不一样
代码太多了,显得文章比较长,懒得看的就直接下载修改后的用就行了:
1.首先修改【OpenLayers\Events.js】
修改OL能够捕获的鼠标事件,添加右键事件“contextmenu”
BROWSER_EVENTS: ["mouseover","mouseout","mousedown","mouseup","mousemove","click","dblclick","rightclick","dblrightclick","resize","focus","blur"//added by lei3389,"contextmenu"//added end],
现在OL已经可以截获鼠标右键了,只是handler没有去处理,下面我们再继续
2.进入【OpenLayers\Handler\Feature.js】
修改这个handler能够处理的事件表
EVENTMAP: {//added by lei'contextmenu': {'in':'click','out':'clickout'},//added end'click': {'in':'click','out':'clickout'},'mousemove': {'in':'over','out':'out'},'dblclick': {'in':'dblclick','out':null},'mousedown': {'in':null,'out':null},'mouseup': {'in':null,'out':null}
},
这里的in和out可以理解为选中和取消选中
这样,这个handler就能够处理contextmenu事件了,但是谁去处理你的事件呢?只能自己添加了:
/**
*added by lei3389!!!
* Method: contextmenu
* Handle contextmenu. Call the "contextmenu" callback if contextmenu on a feature,
* or the "clickout" callback if rightclick outside any feature.
*
* Parameters:
* evt - {Event}
*
* Returns:
* {Boolean}*/contextmenu:function(evt) {returnthis.handle(evt)?!this.stopClick :true;
},
这样就交给handle去处理了
触发选中的本来是左键单击和左键双击,现在我们添加上右击
handle:
{
//var click = (type == "click" || type == "dblclick");//moded by lei//for right click on featurevarclick=(type=="click"||type=="dblclick"||type=="contextmenu");//moded end
}
我们添加右键点击只是利用左键点击触发的事件,所以我们要在触发的事件中添加一个参数,以区分是左键还是右键
if(previouslyIn&&inNew) {//out of last feature and in to anotherthis.triggerCallback(type,'out', [this.lastFeature]);//this.triggerCallback(type, 'in', [this.feature]);//moded by lei3389//for add a paramthis.triggerCallback(type,'in', [this.feature,type]);//end}elseif(!previouslyIn||click) {//in feature for the first time//this.triggerCallback(type, 'in', [this.feature]);//moded by lei3389//for add a paramthis.triggerCallback(type,'in', [this.feature,type]);//end}
3.进入【OpenLayers\Control\SelectFeature.js】
handler只是为control服务的,我们让handler处理了右键事件也是为control处理的
刚刚添加的参数就要有用了:
/**
* Method: clickFeature
* Called on click in a feature
* Only responds if this.hover is false.
*
* Parameters:
* feature - {}*///clickFeature: function(feature)//moded by lei3389//for add a paramclickFeature:function(feature,triggertype) {//endif(!this.hover) {varselected=(OpenLayers.Util.indexOf(
feature.layer.selectedFeatures, feature)>-1);if(selected) {if(this.toggleSelect()) {this.unselect(feature);
}elseif(!this.multipleSelect()) {this.unselectAll({except: feature});
}
}else{if(!this.multipleSelect()) {this.unselectAll({except: feature});
}//moded by lei3389//this.select(feature);this.select(feature,triggertype);//end}
}
},
然后这里也得添加一个参数:
/**
* Method: select
* Add feature to the layer's selectedFeature array, render the feature as
* selected, and call the onSelect function.
*
* Parameters:
* feature - {}*///moded by lei3389//for add a param ---triggertype//select: function(feature)select:function(feature,triggertype){//endvarcont=this.onBeforeSelect.call(this.scope, feature);varlayer=feature.layer;if(cont!==false) {
cont=layer.events.triggerEvent("beforefeatureselected", {
feature: feature
});if(cont!==false) {
layer.selectedFeatures.push(feature);this.layerData={};varselectStyle=this.selectStyle||this.renderIntent;
layer.drawFeature(feature, selectStyle);
layer.events.triggerEvent("featureselected", {feature: feature});//moded by lei3389//for add another "handler"//this.onSelect.call(this.scope, feature);switch(triggertype){case"click":this.onSelect.call(this.scope, feature);break;case"contextmenu":this.onRightSelect.call(this.scope, feature);break;default:this.onSelect.call(this.scope, feature);
}//end}
}
},
当然还得添加一个属性,为使用我们刚才修改的成果提供一个调用的接口
Code/*************************
*added by lei3389!!!!
*APIProperty: onRightSelect
*{Function} Optional function to be called when a feature is selected by RighClick of mouse .
*The function should expect to be called with a feature.*/onRightSelect:function(){},
4.现在对OL库的修改已经over了,我们自己使用的时候就可以用了
到自己的代码中来
首先屏蔽掉默认的右键:
//forbid the mouse's default rightclick
map.div.oncontextmenu = function () { return false;}
然后就可以像定义左键一样来定义鼠标右键了:
sample:
//add selectStationControlselectFeatureControl=newOpenLayers.Control.SelectFeature([VectorLayer1,VectorLayer2,VectorLayer3],
{onSelect: onFeatureSelect,
onUnselect: onFeatureUnselect,
onRightSelect: onFeatureRightSelect
});
map.addControl(selectFeatureControl);
selectFeatureControl.activate();
5.应该这样就对右键的修改就over了吧,我把我自己用的OL上传了.
其中包括了SelectFeature控件对多层选择的支持+右键支持
另外还有和本文不相干的一点修改:
是关于ol解析GML v3.0的posList节点的问题,也可能是esri生成gml的时候esri本身的问题,但是arcgis的源代码我改不了。。只能对OL动手了~~
【OpenLayers.Format.GML】
linestring:function(node, ring) {/**
* Two coordinate variations to consider:
* 1) x0 y0 z0 x1 y1 z1
* 2) x0, y0, z0 x1, y1, z1*/vardim=parseInt(nodeList[0].getAttribute("dimension"));/**
* added by lei3389
*dim will be 'NAN' for esri didnot give GML V3 the dimension attribute!*/if(dim='NAN')dim=2;/**
* end*/
6.终于完了,好长啊
OpenLayers2.7 lei3389修改版下载地址:
http://files.cnblogs.com/lei3389/OpenLayers27_ModedBy_lei3389.rar
openlayer右键菜单_让OpenLayers的SelectFeature控件支持鼠标右键事件相关推荐
- 添加右键菜单_笔记本没有灭屏键?巧在右键菜单添加“关闭显示器”选项
MS酋长的笔记本没有单独的关闭显示器按钮,虽然可以通过运行命令关闭显示器,但毕竟操作比较麻烦.其实我们只需编辑注册表,把关闭显示器命令添加到其中,能够在Win10桌面右键菜单中添加一个"关闭 ...
- chart控件支持鼠标滚轮放大缩小_强大的鼠标侧键功能设置工具:X-Mouse Button Control...
Update:以下内容仅适用于 Windows 系统!AutoCAD for mac 用户该怎么办,下次再细说... 作为 AutoCAD 用户,一个好的鼠标,能让工作更加高效和身心健康!所以,一定要 ...
- 梦想CAD控件网页版右键弹出菜单
用户需要右键弹出菜单,则需要响应鼠标事件,右键按下后,弹出菜单分为在命令下运行及在非命令下运行,具体介绍如下 : 一.新建菜单文件 1.新一个文本文件: mxpoptest.mnu,内容如下(此文件为 ...
- 点击 桌面 计算机 空白 不响应,win7桌面空白处右键电脑迟钝怎么办 为什么电脑在桌面点击鼠标右键反应很慢...
win7桌面空白处右键电脑迟钝怎么办 为什么电脑在桌面点击鼠标右键反应很慢我们在使用电脑的时候,总是会遇到很多的电脑难题.当我们在遇到了桌面鼠标右键菜单反应慢的时候,那么我们应该怎么办呢?今天就一起来 ...
- 控件把鼠标放上去出现一个小窗口_控件跟着鼠标走,VBA让你的操作随心所欲
大家好,我们今日继续讲解VBA代码解决方案的第97讲内容:控件跟随鼠标,在利用VBA写代码的时候,往往很多发现是不经意间的.对于这类发现要善于总结,善于利用才能逐渐的提高自己的能力.例如在前几篇文章中 ...
- openlayers图层开关控件
openlayers2自带图层开关控件,但是自openlayers3后,不再有这个控件.但是,当了解了openlayers控件开发后,我们可以自己实现这个控件,实现起来也非常之简单.不多说,先看下结果 ...
- openlayer右键菜单_使用OpenLayers3 添加地图鼠标右键菜单
添加右键菜单,首先我们要监听鼠标右键点击的操作,我们知道鼠标右键事件名是 contextmenu,当鼠标在 html 元素之上,点击鼠标右键,便会触发 contextmenu 事件,在 context ...
- wxpython 右键菜单_使用wxPython打造印象笔记(14)笔记本管理
上一篇文章中,我们用peewee对数据库建模并创建了数据库,然后成功的加载了默认笔记本.这篇文章将完成笔记本的管理功能,分为创建笔记本,修改笔记本,删除笔记本这三个功能.创建笔记本编辑笔记本删除笔记本 ...
- java 右键菜单_界面操作--添加右键菜单
[java]代码库package 添加右键菜单; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; impor ...
最新文章
- python的工资为什么这么低-为什么学完Python找不到工作?原因如下!
- 遭遇OutOfMemoryError
- windows不能改密码
- 关于Expdp/Impdp 并行导入导出详细测试结果和并行参数的正确理解!!
- 初识Anrdiod SDK
- 一个能自动搜索源文件并自动推导的Makefile
- 【算法设计与分析】06 几类重要的函数
- 高斯课堂数电讲义笔记_【法考经验贴】40岁三战主观题127分!他的笔记学习法助他逆袭!...
- 轻量级日志系统Loki原理简介和使用
- Java复习提纲之面向对象
- Android开源库
- Linux 下编译安装OpenCV【转】
- Python~FTP文件下载
- 最新kali之masscan
- 在 Linux 中配置 tftpboot 服务器的 10 个步骤
- nvidia旧版驱动_鸡血驱动已发布,可新买的显卡打不上驱动?这事儿别急
- 小巧灵便的桌面工具带有便签、闹钟、任务管理功能
- 如何快速实现移动 App 对智能设备的连接和控制,打造丰富的智能生活场景?
- 如何实现MySQL增量备份与恢复?
- 域名系统几类服务器,域名系统