mapbox / leaflet-omnivore

universal format parser for Leaflet & Mapbox.js
https://www.mapbox.com/mapbox.js/example/v1.0.0/omnivore-gpx/
Other
650 stars 126 forks source link

An hello word to use bind bindPopup #8

Closed aborruso closed 10 years ago

aborruso commented 10 years ago

Hi, each function returns an L.geoJson object, but I'm not able to use onEachFeature to have a popup for each marker of my maps.

I have tested the below code, but it does not work.

How to have a popup for an omnivore marker?

Thank you

function onEachFeature(feature, layer) {
        if (feature.properties && feature.properties.mypop) {
            layer.bindPopup(feature.properties.mypop);
        }
    }
    var points = omnivore.geojson('poi.geojson')
    .on('ready', function() {
        map.fitBounds(points.getBounds());
        map.addLayer(points );
        onEachFeature: onEachFeature
    });
tmcw commented 10 years ago

I'll make an example to demonstrate this, but the short answer is (code not tested, but should work):

function eachLayer(layer) {
    var feature = layer.toGeoJSON();
    if (feature.properties && feature.properties.mypop) {
        layer.bindPopup(feature.properties.mypop);
    }
}

var points = omnivore.geojson('poi.geojson')
    .on('ready', function() {
        map.fitBounds(points.getBounds());
        map.addLayer(points );
        points.eachLayer(eachLayer);
    });
aborruso commented 10 years ago

Hi @tmcw it works: http://aborruso.github.io/mapbox_csv

Thank you very much