简体   繁体   中英

OpenLayers add Marker and Popup

I'm having trouble with 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 . 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.

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 . 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. See OpenLayers documentation dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.addPopup or a definition of this method here . 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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