简体   繁体   English

OpenLayers添加标记和弹出窗口

[英]OpenLayers add Marker and Popup

I'm having trouble with OpenLayers. 我在使用OpenLayers时遇到麻烦。 My working code is: 我的工作代码是:

<html><body>
  <div id="mapdiv"></div>
  <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
  <script>
    map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());

    //var results = new OpenLayers.Layer.Text("My Points", { location:"./checkIns_0_view.txt", projection: map.displayProjection});
    //map.addLayer(results);

    var query = new OpenLayers.LonLat(-122.2928337167, 37.5549570333).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    var markers = new OpenLayers.Layer.Markers("Markers");
    map.addLayer(markers);
    var size = new OpenLayers.Size(21,25);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('http://openlayers.org/dev/img/marker-blue.png', size, offset);
    marker = new OpenLayers.Marker(query, icon);
    markers.addMarker(marker);

    var zoom=16;
    map.setCenter (query, zoom);
  </script>
</body></html>

Now I want to add a Popup with some informations. 现在,我想添加一些信息的弹出窗口。 I tried using several examples, like http://openlayers.org/dev/examples/osm-marker-popup.html . 我尝试使用几个示例,例如http://openlayers.org/dev/examples/osm-marker-popup.html I want to add something like this: 我想添加如下内容:

    var popup = new OpenLayers.Popup.FramedCloud("Popup", query, null, "Text", true);
    map.addPopup(popup);

The first line can be compiled, but when I add the second line, the browser doesn't show my map. 第一行可以编译,但是当我添加第二行时,浏览器不会显示我的地图。 I think it might have to do with the query-lonLat, but I doesn't have the necessary OpenLayers-skills to find out. 我认为这可能与query-lonLat有关,但是我没有必要的OpenLayers技能来查找。

I would appreciate an answer very much. 我非常希望得到一个答案。

Greetings 问候

  1. According OpenLayers documentation , you are missing in a popup constructor the anchor parameter between "Text" and true . 根据OpenLayers文档 ,您会在弹出构造器中缺少“ Text”true之间的定位参数 Probably this is the source of your problem. 可能这就是问题的根源。 This parameter has null value in the example for a popup: 在弹出窗口的示例中,此参数的值为空

     var popup = new OpenLayers.Popup.FramedCloud("Popup", myLocation.getBounds().getCenterLonLat(), null, 'We ' + 'could be here. Or elsewhere.', null, true // true if we want a close (X) button, false otherwise ); 

    In Your case you can to use variable icon instead of null value. 在您的情况下,可以使用变量图标代替空值。

  2. In function map.addPopup(popup) you should have second parameter exclusive as well. 在函数map.addPopup(popup)中,您还应该具有第二个参数独占 See OpenLayers documentation dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.addPopup or a definition of this method here . 见的OpenLayers文档dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.addPopup或该方法的定义在这里 I think it is a good practice to use all defined parameters, because it often creates problems. 我认为使用所有定义的参数是一个好习惯,因为它经常会引起问题。

Hopefully it will work, after adding all parameters. 希望在添加所有参数之后它可以工作。 Your code for a working popup: 您的工作弹出窗口代码:

var popup = new OpenLayers.Popup.FramedCloud("Popup", query, null, "Text", null, true);
map.addPopup(popup, false);

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

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