Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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",
"http://vmap0.tiles.osgeo.org/wms/vmap0?", {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",
    "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});
               }

createMap();

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

Thanks.

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
add comment

1 Answer

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",
        "http://vmap0.tiles.osgeo.org/wms/vmap0?", {
            layers: 'basic'
    });

    // Add layer to map
    map.addLayers([wms]);
}
share|improve this answer
    
Thanks, this works perfectly. –  againstflow Feb 22 '13 at 12:45
add comment

Your Answer

 
discard

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.