1

I am trying to create a code where a feature is created, and then automatically a popup window will open. Is there anyone who can assist me please?

Here is a part of the code and I want the popup to open right after that one.

var draw = new OpenLayers.Control.DrawFeature(
wfs_layer, OpenLayers.Handler.Point,
    {
        title: "Create a feature",
        displayClass: "olControlDrawFeaturePolygon",
        multi: true
    });

Later on I have a code for the popup:

//---POPUP fyrir Attribute---//
var select = new OpenLayers.Control.SelectFeature(wfs_layer,{hover: true, highlightOnly: true});
  map.addControl(select);
  select.activate();

    var selectClick = new OpenLayers.Control.SelectFeature(wfs_layer,{});
  map.addControl(selectClick);
  selectClick.activate();

  function onPopupClose(evt) {
  select.unselectAll();
}

wfs_layer.events.on({
featureselected: function(event) {
    var feature = event.feature;
        feature.popup = new OpenLayers.Popup.FramedCloud
            ("pop",
      feature.geometry.getBounds().getCenterLonLat(),
      null,
                '<form name="inputform">'+
                '<table><tr><td align="left">NAME:</td>\
                <td align="left"><input name="name" type="text" size="30" maxlength="30" value="'+feature.attributes.nafnfitju+'" ><br></td></tr>'+
                '<tr><td align="left">Heimild:</td>\
                <td align="left"><input name="source" type="text" size="30" maxlength="30" value="'+feature.attributes.heimild+'" ><br></td></tr>'+
                '<tr><td align="left">Code1:</td>\
                <td align="left"><input name="addrattara" type="text" size="30" maxlength="30" value="'+feature.attributes.addrattara+'" ><br></td></tr>'+
                '<tr><td align="left">Code2:</td>\
                <td align="left"><input name="Source1" type="text" size="30" maxlength="30" value="'+feature.attributes.source1+'" ><br></td></tr>'+
                '<tr><td align="left">Open/closed:</td>\
                <td align="left"><input name="open_closed" type="text" size="30" maxlength="30" value="'+feature.attributes.open_closed+'" ><br></td></tr></table></form>'+
                '<p align="left"><input type="submit" value="Save" onClick="javascript:ourfunction(\''+feature.fid+'\')"></p>'+
                '<p align="right"><input type="submit" value="Delete" onClick="javascript:delete(\''+feature.fid+'\')"></p>',
      null,
      true,
                onPopupClose
    );
    map.addPopup(feature.popup);  
},

//destroy popup when feature is no longer selected. Prevents showing 2 Popups at the same time
featureunselected: function(event) {
var feature = event.feature;
        map.removePopup(feature.popup);
  feature.popup.destroy();
  feature.popup = null;
}
});

Hopefully someone can assist me, thanks in advance.

Sjonni, Iceland

1 Answer 1

2

Have you checked this out? It seems to me that you can adapt it to meet your needs:

http://openlayers.org/dev/examples/select-feature-openpopup.html

I put here a live demo of the example above with some comments/explanations, so you can play with it:

http://jsfiddle.net/Gery/5bNfv/7/

Hope this helps,

4
  • I have checked this one out and it seems like a good thing to use. But my Openlayers knowledge is still pretty bad and I am just semi beginner in this. I want the popup to appear when I create a new feature. I will keep on trying and try to get this working ;) I will keep you informed.
    – Sjonni
    Commented Jan 22, 2014 at 21:02
  • @Sjonni please I updated my answer with a live demo, so you can play with it and learn how to work with OL =)
    – Gery
    Commented Jan 22, 2014 at 22:18
  • Thanks Gery. I appreciate this and will keep on playing and learning how to use OL ;)
    – Sjonni
    Commented Jan 23, 2014 at 9:14
  • 1
    @Sjonni you're welcome =) just to say that all this stuff is confusing at the beginning, the trick is to understand which are the variables and how they move with the functions, think on variables as the passengers of a bus, where the function is the bus, so many buses many stops and many changes =). Also start changing one thing at the time, comment for instance one line (or variables or function) and then copy that one and play with it. You should play at first with the javascript section of the above example, that's the main part, cheers!
    – Gery
    Commented Jan 23, 2014 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.