-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
113 lines (100 loc) · 2.82 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
<!DOCTYPE html>
<html>
<head>
<title>Dynamic information box</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="leaflet.css">
<script src="leaflet.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
.info {
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
width: 10em;
}
.info h4 {
margin: 0 0 5px;
color: #777;
}
.info #currentTown {
margin: 6px 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
let map = L.map("map").setView([32, 35], 8);
L.tileLayer("https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}.png", {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="https://carto.com/attribution/">CartoDB</a>',
subdomains: "abcd",
maxZoom: 19
}).addTo(map);
// Information box
let info = L.control({position: "topright"});
info.onAdd = function() {
let div = L.DomUtil.create("div", "info");
div.innerHTML = '<h4>Towns in Israel</h4><p id="currentTown"></p>';
return div;
};
info.addTo(map);
let geojson;
function towns_color(d) {
if(d > 2089.8) return "#b30000";
if(d > 933.2) return "#e34a33";
if(d > 642) return "#fc8d59";
if(d > 399) return "#fdcc8a";
return "#fef0d9";
}
function towns_style(feature) {
return {
fillColor: towns_color(feature.properties.pop_2015),
weight: 0.5,
opacity: 1,
color: "black",
fillOpacity: 0.7
};
}
let highlightStyle = {
weight: 5,
color: "yellow",
fillOpacity: 0.5
};
let info_p = document.getElementById("currentTown");
function highlightFeature(e) {
e.target.setStyle(highlightStyle);
e.target.bringToFront();
info_p.innerHTML =
e.target.feature.properties.name_eng + "<br>" +
e.target.feature.properties.pop_2015 + " people";
}
function resetHighlight(e) {
geojson.resetStyle(e.target);
info_p.innerHTML = "";
}
fetch("towns.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
geojson = L.geoJSON(data, {
style: towns_style,
onEachFeature: function(feature, layer) {
layer.addEventListener("mouseover", highlightFeature);
layer.addEventListener("mouseout", resetHighlight);
}
}).addTo(map);
});
</script>
</body>
</html>