Andrew Christensen
Andrew Christensen

Reputation: 379

randomly getting OpenLayers.Layer.OSM.Mapnik is not a constructor

I have a small popup window that I am loading on user click of a link on the page. But about half the time, I am getting "OpenLayers.Layer.OSM.Mapnik is not a constructor" in the console.log of firebug. If I close the popup (which is a jqueryui dialog) and then click the link again, it likely won't error. But if I close it and open it again I get the error again. It's very strange.

If I completely refresh the page, I get the error on the first click but close and then I get the map.

Is there some secret to loading the map in a smaller window that I am missing? I don't get the error in the full screen version of the map on a different page...

Here's the code that loads the map:

var map = undefined,
popup = undefined;

function initialize (){
    var center_lat = "39.828175";
    var center_long = "-98.579500";
        OpenLayers.ImgPath = "/images/openlayers/";
    map = new OpenLayers.Map ("map", {
        controls:[
            new OpenLayers.Control.Navigation(),
            new OpenLayers.Control.PanZoomBar()],
        });
    var scalebar = new OpenLayers.Control.ScaleBar({displaySystem: "english",align: "right"});// this is an external library that is loaded as a js file and works great on the full screen map too....
    map.addControl(scalebar);

    layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Open Street Maps");
    map.addLayer(layerMapnik);
    lonLat = OLL(center_long, center_lat);
    map.setCenter (lonLat, 13);
}

Upvotes: 1

Views: 2385

Answers (1)

Niklas Wulff
Niklas Wulff

Reputation: 3524

It could be that you execute your code before the js file with the OSM constructor has loaded completely. This is common if you don't wait for the document.onReady event before executing your code. The first time you load the page the js file is requested from the server, taking a few tenths of a second to load, making it too late to use in the page code. The second time the js code is cached, making it accessible for the page code.

When do you call your initialize() function?

Upvotes: 1

Related Questions