Anish
Anish

Reputation: 391

Mapbox issue with parsing json data for heatmap

My code is this

heat = L.heatLayer([], { maxZoom: 12 }).addTo(map);
 $.getJSON("js/example-single.geojson", function(data) {
    var geojsosn = L.geoJson(data, {
      onEachFeature: function (feature, layer) {
          console.log(feature.geometry.coordinates[0] ,feature.geometry.coordinates[1]);
        heat.addLatLng(feature.geometry.coordinates[0], feature.geometry.coordinates[1]);

      }
    });

but am getting an error "Uncaught TypeError: Cannot read property 'lat' of undefined"

please tell how to fix this, if my code is wrong somebody show me how to parse json data for heat map in mapbox

my json data is

{ "type": "FeatureCollection",
  "features": [
    { "type": "Feature",
      "geometry": {"type": "Point", "coordinates": [13.353323936462402, 38.11200434622822]},
      "properties": {"marker-color": "#000"}
    }
  ]
}

Upvotes: 1

Views: 857

Answers (1)

Heikki
Heikki

Reputation: 15417

addLatLng probably expects L.latLng objects or something that has lat & lng properties.

var heat = L.heatLayer([], { maxZoom: 12 }).addTo(map);
$.getJSON('js/example-single.geojson', function(data) {
    var geojson = L.geoJson(data, {
        onEachFeature: function(feature, layer) {
            feature.geometry.coordinates.forEach(function(p) {
                heat.addLatLng(L.latLng(p[0], p[1]));
            });
        }
    });
});

Upvotes: 2

Related Questions