-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
147 lines (130 loc) · 5.38 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>csv2map</title>
<link rel="stylesheet" href="leaflet/leaflet.css" />
<script src="leaflet/leaflet.js"></script>
<script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>
<style>
#map {
width: 100%;
height: 600px;
padding: 0;
margin: 0;
}
</style>
<div id="map"></div>
<script>
// data source : https://lite.framacalc.org/dan8d6zlfh-9vuw
// load ethercalc data from id
function loadDataFromEthercalcId(id) {
$.ajax({
url: "https://lite.framacalc.org/_/" + id + "/csv.json",
dataType: "text",
success: function(data) {
var addresses = JSON.parse(data)
//console.log(addresses)
for (var i = 0; i < addresses.length; i++) {
var address = addresses[i]
var title = address[3]
var addressSearchString = address[0] + "+" + address[1] + "+" + address[2]
addressSearchString = addressSearchString.replace(/ /g,"+");
console.log("searched string : ", addressSearchString)
console.log("results : ", addressSearchString)
addToMap(addressSearchString,title);
}
}
});
}
function addToMap(address, title = "") {
var options = "&autocomplete=0&addressdetails=1&limit=1";
var url = "https://api-adresse.data.gouv.fr/search/?q=" + address + options;
console.log(url)
$.ajax({
url: url,
dataType: "text",
success: function(data) {
var results = JSON.parse(data)
//console.log(results)
if (results.features.length > 0) {
var feature = results.features[0]
var lat = feature.geometry.coordinates[1]
var lon = feature.geometry.coordinates[0]
console.log(lat + " " + lon);
addMarkerLeaflet(map, lat, lon, title);
}else{
console.log("error, returned empty",address)
}
}
})
}
/*// get coordinates from address using french geocoding service
function addToMap(address, title = "") {
var options = "&autocomplete=0&addressdetails=1&limit=1";
var url = "https://api-adresse.data.gouv.fr/search/?q=" + address + options;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var json = JSON.parse(xhr.responseText);
if (json.features.length > 0) {
var lat = json.features[0].geometry.coordinates[1];
var lon = json.features[0].geometry.coordinates[0];
console.log(lat + " " + lon);
addMarkerLeaflet(map, lat, lon, title);
}else{
console.log("error, returned empty",address)
}
}
}
xhr.send();
}*/
function addMarkerLeaflet(map, lat, lon, title) {
var marker = L.marker([lat, lon]).addTo(map);
if (title.length > 0){
marker.bindPopup(title).openPopup();
}
}
function initLeafletMap(divId){
var map = L.map(divId).setView([46.130026,3.432393], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
return map;
}
var map = null;
$(document).ready(function() {
map = initLeafletMap("map");
//readAddressesFromCSV("data.csv");
loadDataFromEthercalcId("dan8d6zlfh-9vuw");
});
function readAddressesFromCSV(fileName){
var xhr = new XMLHttpRequest();
xhr.open("GET", fileName, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log(xhr.responseText.length);
var lines = xhr.responseText.split("\n");
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.length > 0) {
var fields = line.split(";");
var title = fields[3]
address = fields[0]+","+fields[1]+","+fields[2];
address = address.replace(/ /g,"+");
console.log("searched string : ", address)
addToMap(address,title);
}
}
}
}
xhr.send();
}
</script>
</body>
</html>