简体   繁体   English

OpenLayers不允许我添加弹出窗口

[英]OpenLayers not letting me add Popup

So I have (or had) a working version of OpenStreetMaps, but now that I want to add popups onto the map, the whole thing breaks. 因此,我拥有(或拥有)OpenStreetMaps的工作版本,但是现在我想在地图上添加弹出窗口,整个过程就坏了。 This is the code pertaining to the issue of the popup. 这是与弹出窗口有关的代码。 The crazy thing is that I copy and pasted the code from the official wiki in order to just get a working example. 疯狂的事情是,我复制并粘贴了来自官方Wiki的代码,以便得到一个可行的示例。

function init() {

    map = new OpenLayers.Map( 'heatmapArea');

    var query = new OpenLayers.LonLat(-122.2928337167, 37.5549570333).transform(
                  new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    var popup = new OpenLayers.Popup.FramedCloud("Popup", query, null, "Text", null, true);
    map.addPopup(popup, false);

    var lat = 39.3138895;
    var lon = -98.2233523;
    var zoom = 4;
    var position = new OpenLayers.LonLat(lon, lat).transform( EPSG_WGS84, EPSG_900913);
    map.setCenter(position, zoom );

}

The issue as it appears in my browser console is: 在我的浏览器控制台中出现的问题是:

错误

I have removed the code which I don't think is relevant to this issue but I could provide more if that is necessary. 我已删除了与该问题无关的代码,但是如果有必要,我可以提供更多代码。 I have googled around extensively and all of the examples that I find work fine on the website I visit, but breaks my map and every StackOverflow answer to somebody else seems to work fine for the original poster, but once again, breaks my map. 我到处搜寻了很多例子,但我在访问的网站上找到的所有示例都可以正常工作,但是破坏了我的地图,对于其他人的每一个StackOverflow答案对于原始海报似乎都行得通,但又一次破坏了我的地图。

Here's one of the website I tried to copy: 这是我尝试复制的网站之一:

http://www.rigacci.org/openlayers/other_examples/markers.html http://www.rigacci.org/openlayers/other_examples/markers.html

I am very eager to get this problem solved and any help would be greatly appreciated. 我非常渴望解决此问题,我们将不胜感激。 Thanks! 谢谢!

-CJ -CJ

Someone who really knows their way around the OL API will be able to explain this properly, but basically, your code is fine, but you need to reorder it. 真正了解OL API方式的人将能够正确解释这一点,但是基本上,您的代码很好,但是您需要对其重新排序。 You need to add a map layer, and zoom to an extent, before you can call addPopup. 您需要先添加地图图层并缩放一定程度,然后才能调用addPopup。 I think this is because addPopup doesn't need an explicit layer of its own; 我认为这是因为addPopup不需要自己的显式层。 it uses the map layer; 它使用地图图层; and therefore you need a map layer on your map before trying to use it. 因此您在使用地图之前需要在地图上放置一个地图图层。 That makes sense, but I am not sure why you need also to have called a zoom/zoomToExtent function. 这是有道理的,但是我不确定为什么还需要调用zoom / zoomToExtent函数。

Here's a fiddle, I've tried to leave your code as unchanged as possible: 这是一个小提琴,我试图让您的代码尽可能保持不变:

http://jsfiddle.net/sifriday/u3j6h97d/3/ http://jsfiddle.net/sifriday/u3j6h97d/3/

And here's the JS with some comments: 这是带有一些注释的JS:

function init() {

    var map = new OpenLayers.Map( 'heatmapArea');
    // Add a map layer before trying to use addPopup
    map.addLayer(new OpenLayers.Layer.OSM());

    // Call the zoom function before trying to use addPopup
    var lat = 39.3138895;
    var lon = -98.2233523;
    // I've changed the zoom to 1 so you can immediately see the popup in the small fiddle window
    var zoom = 1;
    var position = new OpenLayers.LonLat(lon, lat).transform(
        "EPSG_WGS84", "EPSG_900913"
    );
    map.setCenter(position, zoom);

    // Finally here's your addPopup code
    var query = new OpenLayers.LonLat(
        -122.2928337167, 37.5549570333
    ).transform(
        new OpenLayers.Projection("EPSG:4326"), 
        map.getProjectionObject()
    );
    var popup = new OpenLayers.Popup.FramedCloud(
        "Popup", 
        query,
        // I added a size to make it fit in the small fiddle window
        new OpenLayers.Size(100,100),
        "Text",
        null,
        true
    );
    map.addPopup(popup);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM