4

i need to hand the latlng coordinates from my phpPostGIS-served geoJSONLayer as an array over to a new added HeatMap Layer. L.geoJSON has an build-in option: coordsToLatLng( coords )" and a static method for that: coordsToLatLngs( coords, levelsDeep?, reverse? ). I don´t get any errors, but the heat map does´t show up. If i build a new L.heatLayer(coord).addTo(map); outside the function, it works… Does anyone have an idea? This is what i have so far:

    $.getJSON("postgis_geojson.php?geotable=shops&geomfield=the_geom", function(data) {

        L.geoJson(data, {
            pointToLayer: function (feature, latlng) {
                return L.circleMarker(latlng, geojsonMarkerOptions);

            },
             onEachFeature: function (feature, layer) {
                 popupOptions = {maxWidth: 200};
                layer.bindPopup(feature.properties.popupContent);
            },
            coordsToLatLng: function (coords) {
            return new L.heatLayer(coords);
            }

        }).addTo(map);
        });

1 Answer 1

5

In OnEachFeature you can find the coordinates in feature.geometry.coordinates. so

   coords = []; //define an array to store coordinates
   onEachFeature: function (feature, layer) {
             popupOptions = {maxWidth: 200};
            layer.bindPopup(feature.properties.popupContent);
            coords.push(feature.geometry.coordinates);

}

Now you can use the array coords in L.heatLayer

4
  • Great, thanx! I only have one Problem now: The coordinates are in the wrong order, so the heatmap displays in the wrong corner of the world..? if i separate to coords.push(feature.geometry.coordinates[1]; coords.push(feature.geometry.coordinates[0]; my array set is destroyed…?
    – stew
    Jun 13, 2014 at 9:47
  • 2
    …Now i got it by myself. Thecoords.push line needs to be: coords.push([feature.geometry.coordinates[1],feature.geometry.coordinates[0]]); Thanks again!
    – stew
    Jun 13, 2014 at 9:54
  • @stew yes I noticed that also... they parse in LatLong (YX) coordinates, but store and work with LongLat coordinates...
    – manatttta
    Feb 5, 2018 at 13:19
  • 1
    To get an actual leaflet LatLng array, you can use the static L.GeoJSON.coordsToLatLngs function once you have the coords.
    – danwild
    May 9, 2018 at 1:01

Your Answer

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

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