When I try the following code, OpenLayers returns a zero result. What have I done wrong, here?

var map;

function loadMap () {

    // Based on example code: view-source:
    //     http://openlayers.org/dev/examples/vector-formats.html

    map = new OpenLayers.Map(el);
    var googleStreetMap = new OpenLayers.Layer.Google('Google Street Map', 
                                                      { numZoomLevels: 20 });
    var result = map.getNumLayers(); // Result == 0 :sad-panda:            

In the code above, I thought that the map.getNumLayers() should return 1 - considering i've asked to add a google street map.

Why is it returning zero, please?

UPDATE: something else to note: there is also no baseLayer :( I thought this would be the base layer???

1 Answer 1


I think it's because addLayers expects an array of layers. If you just want to add a single layer, you should use map.addLayer instead. Or you could give addLayers what it wants:


I hope that helps.

  • 1
    Also you can write map.addLayers([layer]).
    – drnextgis
    Oct 8, 2012 at 7:14
  • Ha damn!!! Originally I had map.addLayers([layer1, layer2]); but removed the layer2 call .. and therefore forgot to remove the last 's'. DOH DOH DOH! Thanks heaps guys. Totally fixed the problem!
    – Pure.Krome
    Oct 8, 2012 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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