2.7.5 回调事件添加与删除
回调事件添加与删除
回调事件是指三维地图主动与前端应用进行交互的功能。包括鼠标事件、键盘事件、图层对象更新事件、响应器对象事件、更新对象事件等。
回调事件的一般处理流程是:前端添加回调事件 --> 三维地图触发回调 --> 前端回调函数处理。
调用示例
Javascript调用
JavaScript在处理回调事件时,添加事件时需要调用控件对象的attachEvent
方法,删除事件时需要调用控件对象的detachEvent
方法。
注意: 如果是IE11版本,添加事件时,方法需要改成addEventListener
,删除事件时需要改成removeEventListener
方法。
一般添加时,可以添加自动检测当前IE版本的代码,然后根据IE版本选择相应的事件方法。
//html
<select id="select">
<option value="FireOnLButtonDown">鼠标左键按下</option><option value="FireOnLButtonUp">鼠标左键抬起</option><option value="FireOnLButtonDblClk">鼠标左键双击</option>
<option value="FireOnRButtonDown">鼠标右键按下</option><option value="FireOnRButtonUp">鼠标右键弹起</option><option value="FireOnRButtonDblClk">鼠标右键双击</option>
<option value="FireOnMButtonDown">鼠标中键按下</option><option value="FireOnMButtonUp">鼠标中键弹起</option><option value="FireOnMButtonDblClk">鼠标中键双击</option>
<option value="FireMouseHover">鼠标悬停</option><option value="FireOnMouseWheel">鼠标滚轮滚动</option><option value="FireOnMouseMove">鼠标指针移动</option>
<option value="FireOnKeyDown">键盘按键按下</option><option value="FireOnKeyUp">键盘按键抬起</option>
<option value="FireOnLayerNotify">图层更新消息</option><option value="FireOnResponserNotify">响应器更新消息</option><option value="FireOnOperationNotify">更新器更新消息</option>
<option value="FireOnDeserializeNotify">工程文件打开完成回调</option><option value="FireOnFullScreenState">全屏回调</option><option value="FireOnToolsNotify">工具类回调</option>
</select>
//js
//回调实现函数
function callBackFun(x, y){
//此处为处理回调事件,x、y参数为返回值
}
var event;
//添加SDK事件
function addSdkEvent() {
var mySelect = document.getElementById("select");
var value = mySelect.options[mySelect.selectedIndex].value;
event = addEvent(obj, value, callBackFun);//返回值用于IE11版本事件移除
}
//删除SDK事件
function removeSdkEvent() {
var mySelect = document.getElementById("select");
var value = mySelect.options[mySelect.selectedIndex].value;
delEvent(obj, value, callBackFun, event);//第四个参数用于IE11事件删除,其他浏览器传null即可
}
//事件添加
function addEvent(object, type, handle){
var ret = null;
if(getIEVersion() === 11){
ret = addIE11Event(obj, type, handle);
}else if(obj.attachEvent){
ret = obj.attachEvent(type, handle);
}else if(obj.addEventListener){
ret = obj.addEventListener(type, handle);
}else{
//console.log("failed to add event");//ie8不可用
}
return ret;
}
//事件删除
function delEvent(object, type, handle, event){
if(getIEVersion() === 11){
delIE11Event(event);
}else if(obj.detachEvent){
obj.detachEvent(type, handle);
}else if(obj.removeEventListener){
obj.removeEventListener(type, handle, false);
}else{
//console.log("failed to remove event");//ie8不可用
}
}
//ie版本检测
function getIEVersion(){
var win = window;
var doc = win.document;
var input = doc.createElement("input");
var ie = (function(){
if(win.ActiveXObject === undefined) return null;
if(!win.XMLHttpRequest) return 6;
if(!doc.querySelector) return 7;
if(!doc.addEventListener) return 8;
if(!win.atob) return 9;
if(!input.dataset) return 10;
return 11;
})();
return ie;
}
//针对ie11添加事件
function addIE11Event(obj, _strEventId, _functionCallback){
var nameFromToStringRegex =/^functions?([^s(]*)/;
var paramsFromToStringRegex = /(.*)/;
var fcbStr = _functionCallback.toString();
var index = fcbStr.indexOf("{");
var funcStr = fcbStr.substring(0, index);
var NextIndex = funcStr.indexOf("(");
var nextStr = funcStr.substring(NextIndex);
funcStr = funcStr.substring(funcStr.indexOf(" ") + 1);
var handler;
try {
handler = document.createElement("script");
handler.setAttribute("for", obj.id);
}
catch(ex) {
handler = document.createElement('<script for="' + obj.id + '">');
}
handler.event = _strEventId + nextStr;//params;
handler.appendChild(document.createTextNode(funcStr + ";"));
document.body.appendChild(handler);
return handler;
}
//针对ie11删除事件
function delIE11Event(handler){
document.body.removeChild(handler);
}
C++调用
需要添加回调时,可以在窗口界面中选中SDK控件;然后右击打开属性对话框,并在属性对话框中选择控件事件,此时属性对话框中会列出SDK控件包含的所有事件。选择需要添加的事件并双击,便可编写事件处理的代码。下面列出了所有事件的处理接口。
BEGIN_EVENTSINK_MAP(Cf6c4EventDlg, CDialogEx)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 1, Cf6c4EventDlg::FireOnLButtonDownVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 2, Cf6c4EventDlg::FireOnLButtonUpVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 3, Cf6c4EventDlg::FireOnLButtonDblClkVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 4, Cf6c4EventDlg::FireOnMButtonDownVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 5, Cf6c4EventDlg::FireOnMButtonUpVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 6, Cf6c4EventDlg::FireOnMButtonDblClkVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 7, Cf6c4EventDlg::FireOnRButtonDownVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 8, Cf6c4EventDlg::FireOnRButtonUpVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 9, Cf6c4EventDlg::FireOnRButtonDblClkVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 10, Cf6c4EventDlg::FireMouseHoverVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 11, Cf6c4EventDlg::FireOnMouseWheelVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 12, Cf6c4EventDlg::FireOnKeyDownVpsdkctrl1, VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 13, Cf6c4EventDlg::FireOnKeyUpVpsdkctrl1, VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 14, Cf6c4EventDlg::FireOnLayerNotifyVpsdkctrl1, VTS_I4 VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 15, Cf6c4EventDlg::FireOnResponserNotifyVpsdkctrl1, VTS_BSTR VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 16, Cf6c4EventDlg::FireOnOperationNotifyVpsdkctrl1, VTS_I4 VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 17, Cf6c4EventDlg::FireOnDeserializeNotifyVpsdkctrl1, VTS_BOOL)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 18, Cf6c4EventDlg::FireOnFullScreenStateVpsdkctrl1, VTS_BOOL)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 19, Cf6c4EventDlg::FireOnMouseMoveVpsdkctrl1, VTS_I2 VTS_I2)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 20, Cf6c4EventDlg::FireOnToolsNotifyVpsdkctrl1, VTS_I4 VTS_I4)
ON_EVENT(Cf6c4EventDlg, IDC_VPSDKCTRL1, 21, Cf6c4EventDlg::FireOnLicStateVpsdkctrl1, VTS_BOOL)
END_EVENTSINK_MAP()
void Cf6c4EventDlg::FireOnLButtonDownVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnLButtonUpVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"LButton Up. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnLButtonDblClkVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"LButton DblClick. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnMButtonDownVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnMButtonUpVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"MButton Up. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnMButtonDblClkVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"MButton DblClick. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnRButtonDownVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnRButtonUpVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"RButton Up. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
//MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnRButtonDblClkVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"LButton DblClick. x:";
ss.AppendFormat(L"%d; y:%d", xPos, yPos);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireMouseHoverVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnMouseWheelVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnKeyDownVpsdkctrl1(long nChar)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnKeyUpVpsdkctrl1(long nChar)
{
// TODO: 在此处添加消息处理程序代码
CString ss = L"Key Up. char:";
ss.AppendFormat(L"%d", nChar);
MessageBox(ss.GetString());
}
void Cf6c4EventDlg::FireOnLayerNotifyVpsdkctrl1(long layerId, long layerType)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnResponserNotifyVpsdkctrl1(LPCTSTR respType, long notifyType)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnOperationNotifyVpsdkctrl1(long type, long result)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnDeserializeNotifyVpsdkctrl1(BOOL state)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnFullScreenStateVpsdkctrl1(BOOL state)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnMouseMoveVpsdkctrl1(short xPos, short yPos)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnToolsNotifyVpsdkctrl1(long ToolsID, long notifyType)
{
// TODO: 在此处添加消息处理程序代码
}
void Cf6c4EventDlg::FireOnLicStateVpsdkctrl1(BOOL state)
{
// TODO: 在此处添加消息处理程序代码
if (state)
{
MessageBox(L"授权成功.");
}
}
C#调用
//鼠标悬停
private void axVPSDKCtrl1_FireMouseHover(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireMouseHoverEvent e)
{
}
//工程文件打开完成回调
private void axVPSDKCtrl1_FireOnDeserializeNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnDeserializeNotifyEvent e)
{
}
//全屏回调
private void axVPSDKCtrl1_FireOnFullScreenState(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnFullScreenStateEvent e)
{
}
//键盘按键按下
private void axVPSDKCtrl1_FireOnKeyDown(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnKeyDownEvent e)
{
}
//键盘按键抬起
private void axVPSDKCtrl1_FireOnKeyUp(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnKeyUpEvent e)
{
}
//图层更新消息
private void axVPSDKCtrl1_FireOnLayerNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnLayerNotifyEvent e)
{
}
//鼠标左键双击
private void axVPSDKCtrl1_FireOnLButtonDblClk(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnLButtonDblClkEvent e)
{
}
//鼠标左键抬起
private void axVPSDKCtrl1_FireOnLButtonUp(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnLButtonUpEvent e)
{
}
//鼠标左键按下
private void axVPSDKCtrl1_FireOnLButtonDown(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnLButtonDownEvent e)
{
}
//鼠标中键双击
private void axVPSDKCtrl1_FireOnMButtonDblClk(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnMButtonDblClkEvent e)
{
}
//鼠标中键按下
private void axVPSDKCtrl1_FireOnMButtonDown(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnMButtonDownEvent e)
{
}
//鼠标中键弹起
private void axVPSDKCtrl1_FireOnMButtonUp(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnMButtonUpEvent e)
{
}
//鼠标指针移动
private void axVPSDKCtrl1_FireOnMouseMove(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnMouseMoveEvent e)
{
}
//鼠标滚轮滚动
private void axVPSDKCtrl1_FireOnMouseWheel(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnMouseWheelEvent e)
{
}
//更新器更新消息
private void axVPSDKCtrl1_FireOnOperationNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnOperationNotifyEvent e)
{
}
//鼠标右键双击
private void axVPSDKCtrl1_FireOnRButtonDblClk(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnRButtonDblClkEvent e)
{
}
//鼠标右键按下
private void axVPSDKCtrl1_FireOnRButtonDown(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnRButtonDownEvent e)
{
}
//鼠标右键弹起
private void axVPSDKCtrl1_FireOnRButtonUp(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnRButtonUpEvent e)
{
}
//响应器更新消息
private void axVPSDKCtrl1_FireOnResponserNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnResponserNotifyEvent e)
{
}
//工具类回调
private void axVPSDKCtrl1_FireOnToolsNotify(object sender, AxSDKCtrlLib._IVPSDKCtrlEvents_FireOnToolsNotifyEvent e)
{
}