OpenLayers Event modelΒΆ

Every object has predefined event types, which can be assigned to particular function in defined scope. When you write your custom code, you can define your own events and register them, using

this.events = new OpenLayers.Events(this, this.div,
                                    ["event1","event2"]);

For example object OpenLayers.Map, has several predefined events:

preaddlayer
triggered before a layer has been added. The event object will include a layer property that references the layer to be added.
addlayer
triggered after a layer has been added. The event object

will include a layer property that references the added layer.

removelayer
triggered after a layer has been removed. The event object will include a layer property that references the removed layer.
changelayer
triggered after a layer name change, order change, or visibility change (due to resolution thresholds). Listeners will receive an event object with layer and property properties. The layer property will be a reference to the changed layer. The property property will be a key to the changed property (name, visibility, or order).
movestart
triggered after the start of a drag, pan, or zoom
move
triggered after each drag, pan, or zoom
moveend
triggered after a drag, pan, or zoom completes
zoomend
triggered after a zoom completes
addmarker
triggered after a marker has been added
removemarker
triggered after a marker has been removed
clearmarkers
triggered after markers have been cleared
mouseover
triggered after mouseover the map

mouseout triggered after mouseout the map

mousemove
triggered after mousemove the map
dragstart
Does not work. Register for movestart instead.
drag
Does not work. Register for move instead.
dragend
Does not work. Register for moveend instead.
changebaselayer
triggered after the base layer changes

OpenLayers.Layer has:

loadstart Triggered when layer loading starts.

loadend Triggered when layer loading ends.

loadcancel Triggered when layer loading is canceled.

visibilitychanged Triggered when layer visibility is changed.

move Triggered when layer moves (triggered with every mousemove
during a drag).
moveend Triggered when layer is done moving, object passed as
argument has a zoomChanged boolean property which tells that the zoom has changed.

and OpenLayers.Layer.Vector adds:

beforefeatureadded Triggered before a feature is added. Listeners
will receive an object with a feature property referencing the feature to be added. To stop the feature from being added, a listener should return false.
beforefeaturesadded Triggered before an array of features is added.
Listeners will receive an object with a features property referencing the feature to be added. To stop the features from being added, a listener should return false.
featureadded Triggered after a feature is added. The event
object passed to listeners will have a feature property with a reference to the added feature.
featuresadded Triggered after features are added. The event
object passed to listeners will have a features property with a reference to an array of added features.
beforefeatureremoved Triggered before a feature is removed. Listeners
will receive an object with a feature property referencing the feature to be removed.
featureremoved Triggerd after a feature is removed. The event
object passed to listeners will have a feature property with a reference to the removed feature.
featuresremoved Triggered after features are removed. The event
object passed to listeners will have a features property with a reference to an array of removed features.
featureselected Triggered after a feature is selected. Listeners
will receive an object with a feature property referencing the selected feature.
featureunselected Triggered after a feature is unselected.
Listeners will receive an object with a feature property referencing the unselected feature.
beforefeaturemodified Triggered when a feature is selected to
be modified. Listeners will receive an object with a feature property referencing the selected feature.
featuremodified Triggered when a feature has been modified.
Listeners will receive an object with a feature property referencing the modified feature.
afterfeaturemodified Triggered when a feature is finished being modified.
Listeners will receive an object with a feature property referencing the modified feature.
vertexmodified Triggered when a vertex within any feature geometry
has been modified. Listeners will receive an object with a feature property referencing the modified feature, a vertex property referencing the vertex modified (always a point geometry), and a pixel property referencing the pixel location of the modification.
sketchstarted Triggered when a feature sketch bound for this layer
is started. Listeners will receive an object with a feature property referencing the new sketch feature and a vertex property referencing the creation point.
sketchmodified Triggered when a feature sketch bound for this layer
is modified. Listeners will receive an object with a vertex property referencing the modified vertex and a feature property referencing the sketch feature.
sketchcomplete Triggered when a feature sketch bound for this layer
is complete. Listeners will receive an object with a feature property referencing the sketch feature. By returning false, a listener can stop the sketch feature from being added to the layer.
refresh Triggered when something wants a strategy to ask the protocol
for a new set of features.

and so on. If we want to do something, when new feature is added to the vector layer, it is simple:

var vlayer = new OpenLayers.Layer.Vector("Vector layer");

var onFeatureAdd = function(e) {
    // do something
    // 'this' (scope) is {self: object, layer: vector layer}
};

vlayer.events.register("featureadded",{"self":this,layer:vlayer},onFeatureAdd);

As clear from the example above, events.register function accepts three parameters:

  • event name

  • scope, in which the target function will be called (usually parent

    object of the method

  • function, which will be called

Main Menu


Previous topic

Editing vectors

Next topic

The little things

This Page