Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - Leaflet Heatmap Error ( Dynamic longitudes and latitudes)

I have a 2 dimensional array location which contains the latitudes and longitudes that I would like to present in my heatmap. I created 2 additional arrays from location:

for(var i=0;i<location.length;i++){
  lat_array=location[i][0]
  long_array=location[i][1]
};

let testData = {
  max: 8,
  data: [{lat:lat_array, lng:long_array, count:8}]
}; //Count is random
     
let cfg={
  "radius": 40,
  "maxOpacity": 0.8,
  "scaleRadius": false,
  "useLocalExtrema": false,
  latField: 'lat',
  lngField:'lng',
  valueField:'count'
};

let heatmapLayer =  new HeatmapOverlay(cfg);

mymap.addLayer(heatmapLayer);
heatmapLayer.setData(testData);

When I run the page I get an error message saying that the longitude and latitude cannot be arrays and I tried running it with specific values (lat_array[1],long_array[1]) and it worked. So I am trying to figure out how to add all of the latitudes and longitudes in the location array which its values vary each time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your data format is not correct. You have an array with a single object, whose lat and lng properties are arrays of lats and lngs. You need an array of objects, each object of the format:

[
  {
    lat: latitude_value,
    lng: longitude_value,
    count: count
  },
  {
    lat,
    lng,
    count,
  }
  { ... },
  ...
]

I'm not sure where you data is coming from, or what format it's in, but you need to manipulate it to get it into this format for the heatmap to work properly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...