OpenLayers学习——事件注册

例一,给map类注册一个事件,查看文档有以下说明:
Register a listener for a particular event with the following syntax
map.events.register(type, obj, listener);
Listeners will be called with a reference to an event object.  The properties of this event depends on exactly what happened.

于是我写了以下测试代码:

map.events.register("mousemove", null, map_mousemove);
function map_mousemove(evt) {
	var obj = evt;
}

测试通过,当鼠标在地图上移动时候,触发了该事件。

这里我有个疑问:注册事件的时候第二个参数究竟有什么用?
经过多次测试发现,在回调函数的参数evt,也没有注册回调函数的第二参数传回来。

例二,给地图量测工具注册一个返回结果的事件。
Measure类继承自Control类,在我看源码的时候发现了在Control类中有这样的一个属性:

    /**
     * APIProperty: eventListeners
     * {Object} If set as an option at construction, the eventListeners
     *     object will be registered with <OpenLayers.Events.on>.  Object
     *     structure must be a listeners object as shown in the example for
     *     the events.on method.
     */
    eventListeners: null,

因此为了实现功能,除了例一中,使用如下代码实现:

	meaTool.events.register("measure", null, measureFunc);
	meaTool.events.register("measurepartial", null, measurepartialFunc);

还可以直接在构造函数中传入事件的回调函数来实现这一功能,代码如下:

	var meaTool = new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
		eventListeners: {
			"measure": measureFunc,
			"measurepartial": measurepartialFunc
			},
		immediate:true
	});

OpenLayers大多数的类都支持上述两种方法来注册回调函数。

转载自:https://blog.csdn.net/devCopper/article/details/26671299

You may also like...