随机获取 OpenLayers.Layer.OSM.Mapnik 不是构造函数

发布于 2024-10-25 17:00:22 字数 1092 浏览 6 评论 0原文

我有一个小的弹出窗口,当用户单击页面上的链接时加载该窗口。但大约一半的时间,我在 firebug 的 console.log 中收到“OpenLayers.Layer.OSM.Mapnik 不是构造函数”。如果我关闭弹出窗口(这是一个 jqueryui 对话框),然后再次单击该链接,它可能不会出错。但如果我关闭它并再次打开它,我会再次收到错误。这很奇怪。

如果我完全刷新页面,第一次单击时会出现错误,但关闭后会得到地图。

在我缺少的较小窗口中加载地图是否有一些秘密?我在不同页面上的全屏版本地图中没有收到错误...

这是加载地图的代码:

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-11-01 17:00:22

您可能在带有 OSM 构造函数的 js 文件完全加载之前执行代码。如果您在执行代码之前不等待 document.onReady 事件,则这种情况很常见。第一次加载页面时,会从服务器请求 js 文件,加载需要零点几秒,因此无法在页面代码中使用。第二次缓存js代码,使其可供页面代码访问。

你什么时候调用你的initialize()函数?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文