There are a lot of different approaches to the problem. What I'm going to describe is just one of them.
First off, I'm gonna use the latest available version of Leaflet:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.2/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
And just one heatmap plugin. There is no reason on earth why you should need two heatmap plugins at the same time.
<script src="https://unpkg.com/leaflet.heat@0.2.0/dist/leaflet-heat.js"></script>
Next off is reading the contents of a text file into a JS variable. There are several methods, but I'm particularly fond of the fetch
API:
fetch('http://something/something/data.csv').then(function(response){
return response.text();
}).then(function(text){
// Handling of the text contents goes here
}).catch(function(err){
// Error handling goes here (e.g. the network request failed, etc)
})
Split the text into lines...
var lines = text.split("\n");
...iterate through the lines, except the first one...
for (var i=1; i<lines.length; i++) {
...split the line into the comma-separated parts (in this case, semicolon-separated); I'm assuming a trivial CSV format (things can get a bit more complicated with some CSV files)...
var parts = lines[i].split(";");
...get the data you want to show on the heatmap, in a form that the heatmap plugin will like, keeping in mind that parts
is a 0-indexed array...
var heatData = []
for(...){
...
// heatData.push( lat, lng, weight )
heatData.push( [ parts[4], parts[5], parts[3] ] )
...and once the loop over the lines is over and heatData
is ready, init the heatmap:
var heat = L.heatLayer(heatData, {radius: 25}).addTo(map);
And put it all together:
var map = new L.Map('map').fitWorld();
var positron = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// I'll just ignore the network request and fake some data.
// fetch('http://something/something/data.csv').then(function(response){
// return response.text();
// }).then(function(text){
text = "id;Código postal;Localidad;Valoracion;lat;lng\n1;46100;Burjassot;8;39.51;-0.425055\n2;18005;Granada;7;37.169266;-3.597161";
var lines = text.split("\n");
var heatData = [];
for (var i=1; i<lines.length; i++) {
var parts = lines[i].split(";");
heatData.push( [ parts[4], parts[5], parts[3] ] );
}
var heat = L.heatLayer(heatData, {radius: 25}).addTo(map);
// }).catch(function(err){
// // Error handling for the fetch goes here (e.g. the network request failed, etc)
// })
See a working example here.
Please do not blindly copy-paste the code. Every time you copy-paste code, the stackoverflow gods kill a kitten and somebody cooks rice with peas and fish on it and calls it paella. Think on the kittens. And the paella.
Please note that I've split the problem is several, smaller problems:
- Choose the right tools (leaflet version, heatmap implementation)
- Read a text file (fetch/ajax/XHR/etc)
- Parse the CSV (split lines from the file, and fields form the lines)
- Create a data structure for the heatmap (loop, choose fields)
Depending on your specific scenario, you might have to modify any of these smaller problems.