Understanding OpenLayers 2 Syntax

This section is intended to get users comfortable with the syntax used when developing an application with OpenLayers 2.

OpenLayers 2 “Classes”

OpenLayers 2 is written in a “classical” style. This means that the library provides functions intended to be used with the new keyword that return objects. These functions all begin with a capital letter.

var map = new OpenLayers.Map("map", options);

The code above creates a new map object with all the properties of the OpenLayers.Map function’s prototype. The properties that are intended to be set or accessed are documented in the API Documentation

In general, all parameters to a constructor are required, except for the options parameter. When this is not the case, the API documentation for the constructor will usually say so.

The options Argument

Most OpenLayers 2 object constructors take an options object as one of their arguments. In general, you can set the value of any API property in a contructor’s options argument.

For example, looking at the API Docs for the Vector layer, you can see the isBaseLayer property. If you specify a value for isBaseLayer in the options argument, this will be set on the layer.

For example:

var layer = new OpenLayers.Layer.Vector("Layer Name", {
    isBaseLayer: true
});

layer.isBaseLayer === true;  // this is true

You can also see that the Vector layer inherits from OpenLayers.Layer. Following the link to the Layer API documentation, you’ll find a reference to the layer’s projection property. As with isBaseLayer, if you provide a projection property in the options argument, this value will be set on the layer.

var layer = new OpenLayers.Layer.Vector("Layer Name", {
    projection: new OpenLayers.Projection("EPSG:900913")
});