Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't know well enough OpenLayers syntax, so I need to ask here. I need to name every function in OpenLayers (for creating map - tiles, for drawing markers on the map etc.). So, I have this simple code:

map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"", {layers: 'basic'});

I would need to put that inside the function and name that function, something like this:

function createMap() {
    map = new OpenLayers.Map('map');
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
    "", {layers: 'basic'});


But that code doesn't work. So, can you tell me if this is possible and why my code doesn't work?


share|improve this question
Where are you calling th3 function from? Usually it is called on the onLoad event. – Devdatta Tengshe Feb 22 '13 at 1:37
up vote 4 down vote accepted

It's always necessary to specify, what "doesn't work" - does it give any error message, etc.

In your createMap() code, you create local variable wms and you don't do anything with it. Probably you want to add it to map:

var map;

function createMap() {
    // map goes into global variable
    map = new OpenLayers.Map('map');

    // wms is local variable, won't be available later
    var wms = new OpenLayers.Layer.WMS(
        "OpenLayers WMS",
        "", {
            layers: 'basic'

    // Add layer to map
share|improve this answer
Thanks, this works perfectly. – againstflow Feb 22 '13 at 12:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.