Using @tmcw's csv2geojson to add points from a CSV to a Mapbox GL JS map.
forked from danswick's block: CSV on a GL map!
xxxxxxxxxx
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.css' rel='stylesheet' />
<script src='csv2geojson.js'></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZGFuc3dpY2siLCJhIjoiY2l1dTUzcmgxMDJ0djJ0b2VhY2sxNXBiMyJ9.25Qs4HNEkHubd4_Awbd8Og';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location
center: [ -89.89709901792442, 41.29146740952274], // starting position
zoom: 3 // starting zoom
});
var getCSV = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader('Accept', 'text/plain');
xhr.onerror = function(e) {
callback(e);
};
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300 && xhr.response) {
var data;
try {
data = xhr.response;
} catch (err) {
return callback(err);
}
callback(null, data);
} else {
callback(new Error(xhr.statusText));
}
};
xhr.send();
return xhr;
};
$(document).ready(function() {
var myUrl = 'https://youthmap-chapter-mapproxy.herokuapp.com/docs.google.com/spreadsheets/d/13yswnN49P0dsO5BS2FJYaVUIjsnuWbzA3UxvhcLUsYI/export?format=csv&id=13yswnN49P0dsO5BS2FJYaVUIjsnuWbzA3UxvhcLUsYI'
var getCSVCallback = function (err, csvdata) {
if (err) { return callback(err) }
csv2geojson.csv2geojson(csvdata, params, callback);
//console.log('data', data, params)
}
getCSV(myUrl, makeGeoJSON);
/*
$.ajax({
type: "GET",
url: 'https://youthmap-chapter-mapproxy.herokuapp.com/docs.google.com/spreadsheets/d/13yswnN49P0dsO5BS2FJYaVUIjsnuWbzA3UxvhcLUsYI/export?format=csv&id=13yswnN49P0dsO5BS2FJYaVUIjsnuWbzA3UxvhcLUsYI',
dataType: "text",
success: function(csvData) {makeGeoJSON(csvData);}
});*/
});
function makeGeoJSON(err, csvData) {
if (err) { console.log(err) }
csv2geojson.csv2geojson(csvData, {
latfield: 'Latitude',
lonfield: 'Longitude',
delimiter: ','
}, function(err, data) {
map.on('load', function () {
map.addLayer({
'id': 'airports',
'type': 'symbol',
'source': {
'type': 'geojson',
'data': data
},
'layout': {
"icon-image": "marker-15"
},
'paint': {}
});
});
});
}
</script>
</body>
</html>
https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js
https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.js