-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·74 lines (66 loc) · 1.73 KB
/
main.js
File metadata and controls
executable file
·74 lines (66 loc) · 1.73 KB
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
import L from "leaflet"
let hashCode = function(str) {
var hash = 0
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}
return hash
}
let intToRGB = function(i){
var c = (i & 0x00FFFFFF).toString(16).toUpperCase()
return "00000".substring(0, 6 - c.length) + c
}
let generateColor = function(str) {
if (str) {
return "#" + intToRGB(hashCode(str))
} else {
return "#cc3300"
}
}
let map = L.map("map").setView([20, 0], 2)
L.tileLayer("https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}.png", {}).addTo(map)
let group = new L.featureGroup().addTo(map)
$("#csv").text(data)
let color = $("#color")
color.on("change", function() {
window.plot()
})
window.plot = function() {
group.clearLayers()
let delimiter = $("#delimiter").val()
let selectedField = color.val()
if (delimiter == "tab") {
delimiter = " "
}
let input = $("#csv").val()
let fields = null
window.csvtojson({ delimiter: delimiter }).fromString(input)
.on("json", (row) => {
if (!fields) {
fields = Object.keys(row)
}
let marker = new L.CircleMarker([row.decimalLatitude, row.decimalLongitude], {
radius: 4,
fillOpacity: 1,
opacity: 1,
color: "white",
fillColor: generateColor(row[selectedField]),
weight: 2,
clickable: true
})
if (selectedField) {
marker.bindPopup(row[selectedField])
}
marker.addTo(group)
})
.on("done", () => {
color.find("option").remove().end().append('<option>Select field</option>').val(null)
fields.forEach(field => {
color.append('<option value="' + field + '">' + field + '</option>');
})
if (fields.indexOf(selectedField) > -1) {
color.val(selectedField)
}
map.fitBounds(group.getBounds())
})
}