1

I have a map. It has two origins, and two destinations. The code for one origin is here:

vectors_origin = new OpenLayers.Layer.Vector("Origin", {
            eventListeners:{
                    'featureselected':function(evt){
                    var feature = evt.feature;
                    var popup = new OpenLayers.Popup.FramedCloud("popup",
                    OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                    null,
                    json.orig1_iata_code.iata+"<br>",
                    null,
                    true,
                    null
                    );
                    popup.autoSize = true;
                    popup.maxSize = new OpenLayers.Size(400,800);
                    popup.fixedRelativePosition = true;
                    feature.popup = popup;
                    map.addPopup(popup);
            },
            'featureunselected':function(evt){
            var feature = evt.feature;
            map.removePopup(feature.popup);
            feature.popup.destroy();
            feature.popup = null;
            }
    }
    });

Note this line:

json.orig1_iata_code.iata+"<br>",

This takes care of one origin. Now since all 4 points (2 origins, 2 destinations), this HTML is going to differ a bit. There will be an

orig1_iata_code, orig2_iata_code, dest1_iata_code, and dest2_iata_code

I really don't want to have to define 4 separate vector layers. Sure it will work, but that seems pretty tacky (correct me if I'm wrong).

So where that html is called above, json.orig1_iata_code.iata+"<br>",, is there any way to specify 'multiple' optional parameters? Multiple I've done before, but all of them will be called every time.

  • It's not clear what you are trying to do. What exactly are orig1_iata_code, orig2_iata_code...? What exactly do you want to achieve? – Devdatta Tengshe Nov 13 '13 at 3:30
  • Is there an attribute that defines which point is what? How do you know to which point orig1_iata_code refers to? – Julien-Samuel Lacroix Nov 13 '13 at 15:18
  • Sorry for not clarifying. orig1_iata_code (and the other 3) are what should be displaying in the popup, which are airport codes (this being a map displaying airports). I came up with a solution: definitely not the most elegant but works. @Julien-SamuelLacroix the points are returned via json array in a php file, referenced in an ajax call in the OL file. – Nubtacular Nov 13 '13 at 18:18
1

I'm not sure what your solution was, but if you can know what variable correspond to what point it could be as simple as:

'featureselected':function(evt){
    var feature = evt.feature;

    var popup_text = '<br>';
    if(feature.attributes.your_attribute == 'orig1') {
        popup_test = orig1_iata_code;
    } else if(feature.attributes.your_attribute == 'orig2') {
        popup_test = orig2_iata_code;
    } else if(feature.attributes.your_attribute == 'dest1') {
        popup_test = dest1_iata_code;
    } else if(feature.attributes.your_attribute == 'dest2') {
        popup_test = dest2_iata_code;
    }

    var popup = new OpenLayers.Popup.FramedCloud("popup",
        OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
        null,
        popup_text,
        null,
        true,
        null
    );
    popup.autoSize = true;
    popup.maxSize = new OpenLayers.Size(400,800);
    popup.fixedRelativePosition = true;
    feature.popup = popup;
    map.addPopup(popup);
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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