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);
        });
stew
  • 73
  • 1
  • 1
  • 6

1 Answers1

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

poshan
  • 761
  • 1
  • 9
  • 26
  • 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 '14 at 09: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 '14 at 09:54
  • @stew yes I noticed that also... they parse in LatLong (YX) coordinates, but store and work with LongLat coordinates... – manatttta Feb 05 '18 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 09 '18 at 01:01