mercredi 6 mai 2015

Leaflet popup closing immediately after function fires to open popup dynamically

I have a feature that allows the user to look up a marker and zoom to its location, at which point I want the popup to open. Everything is working fine, except that the popup closes after the function fires. I can't figure out what's making the popup close. Here's my relevant code:

var geoJsonDataFeatures = {
"type": "FeatureCollection",
"features":[]
};

//create popup content
var htmlString = "<div>popup html goes here....</div>";
var popupContent = L.popup().setContent(htmlString);

//create geoJson object for each marker
var geoJsonData = {
"type": "Feature",
"id": siteNo, //a string saved as a variable 
"properties": {
    "popupContent": popupContent,
    "icon": icon //an L.icon object
    },
"geometry": {
    "type": "Point",
    "coordinates": [longitude, latitude] //strings saved as variables
    }
}

//push the geoJson feature into the features object
geoJsonDataFeatures.features.push(geoJsonData);

//create the map layer that holds the markers
var geoJsonDataLayer = L.geoJson(geoJsonDataFeatures, {
    onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.popupContent);
    layer.setIcon(feature.properties.icon);
    }
}).addTo(map);

....

//when the user has selected a site from a dropdown select menu,
//we pass the site number (siteNo) to a function to zoom and open the
//marker popup:

zoomOpenPopup(siteNo);

function zoomOpenPopup(siteNo){
var marker = {};
var popup = {};
var layerObj = {};

    if(map.hasLayer(geoJsonDataLayer))
    {
    layerObj = geoJsonDataLayer;
    }

    jQuery.each(layerObj._layers, function(k){

        if(siteNo == layerObj._layers[k].feature.id)
        {
        marker = layerObj._layers[k];
        popup = layerObj._layers[k].feature.properties.popupContent;
        }  
    });

marker.openPopup();
//popup._isOpen = true; //doesn't work
//popup.openOn(map); //doesn't work
//map.openPopup(popup); //doesn't work
 alert("done!"); 
}

When the alert fires, you can see the popup open on the map, but once it closes the popup disappears. I do have a function that updates the position of the popup anchor on open if the content is too wide, but I have the same issue even if I comment out that function and reload the page. Has anyone run into something similar?

Aucun commentaire :

Enregistrer un commentaire