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

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:

    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???

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
Also you can write map.addLayers([layer]). – drnextgis Oct 8 '12 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 '12 at 23:54

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.