Skip to content
Advertisement

Get Leaflet pop up to only show when feature properties are not null

I have GeoJSON data that contains URLs. Not all of the features have url data. I have a pop up which shows the name and a link to the url. I’d like to be able to only show the link to URL when the feature URL is not null but will always show the name as a minimum. My code is below:

JavaScript

Advertisement

Answer

Instead of using the bindPopup method with a function, which finds out too late that the feature does not have a URL to show, in which case you actually want no popup, you can leverage the onEachFeature option of the L.geoJSON factory to attach a popup conditionally:

A Function that will be called once for each created Feature, after it has been created and styled. Useful for attaching events and popups to features.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement