-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
173 lines (152 loc) · 4 KB
/
map.js
File metadata and controls
173 lines (152 loc) · 4 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const countriesURL = "./assets/countries.js"; // GeoJSON of the Globe
const airportsURL = "./assets/airports.js"; // Airports Data Set
// Import Data sets countries and airports
$.when(
$.getScript(countriesURL),
$.getScript(airportsURL),
$.Deferred(function (deferred) {
$(deferred.resolve);
})
).done(function () {
/**
* My Data Map with Leaflet
*/
const map = L.map("map").setView([45, 0], 2);
let geoJson;
/* Get airport counts per country */
let airportCount = {};
for (let i = 0; i < airports.length; i++) {
const curAirport = airports[i].Country;
if (!airportCount.hasOwnProperty(curAirport)) {
airportCount[curAirport] = 1;
} else {
airportCount[curAirport]++;
}
}
/**
* Return the color to fill a region with based on weight
* @param {int} weight - weight to determine color
*/
function getColor(weight) {
return weight > 1000
? "#08306b"
: weight > 500
? "#08519c"
: weight > 200
? "#2171b5"
: weight > 100
? "#4292c6"
: weight > 50
? "#6baed6"
: weight > 20
? "#9ecae1"
: weight > 10
? "#c6dbef"
: "#deebf7";
}
/**
* Map Style
* @param {*} feature - region from GeoJSON
*/
function style(feature) {
return {
fillColor: getColor(airportCount[feature.properties.name]),
weight: 2,
opacity: 1,
color: "white",
dashArray: "3",
fillOpacity: 0.7,
};
}
/**
* Apply Event Listeners to Map Layers
*/
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
});
}
/**
* Mouse-In Event
* @param {*} e - event
*/
function highlightFeature(e) {
let layer = e.target;
layer.setStyle({
weight: 5,
color: "#666",
dashArray: "",
fillOpacity: 0.7,
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
/**
* Mouse-Out Event
* @param {*} e - event
*/
function resetHighlight(e) {
geoJson.resetStyle(e.target);
info.update();
}
// Information Bubble
const info = L.control();
// create a div with a class "info"
info.onAdd = function (map) {
this._div = L.DomUtil.create("div", "info");
this.update();
return this._div;
};
// method that we will use to update the control based on feature properties passed
info.update = function (props) {
this._div.innerHTML = props
? "<b>" +
props.name +
"</b><br />" +
airportCount[props.name] +
" airports"
: "Hover over a country for more information";
};
// Map Legend
const legend = L.control({ position: "bottomright" });
legend.onAdd = function (map) {
let div = L.DomUtil.create("div", "info legend"),
grades = [0, 10, 20, 50, 100, 200, 500, 1000],
labels = [];
// loop through our density intervals and generate a label with a colored square for each interval
for (let i = 0; i < grades.length; i++) {
div.innerHTML +=
'<i style="background:' +
getColor(grades[i] + 1) +
'"></i> ' +
grades[i] +
(grades[i + 1] ? "–" + grades[i + 1] + "<br>" : "+");
}
return div;
};
// Init Map
L.tileLayer(
"https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
{
attribution:
'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: "mapbox/light-v11",
tileSize: 512,
zoomOffset: -1,
accessToken:
"pk.eyJ1IjoiaGNzaGlyZXMiLCJhIjoiY2wycWx4c3ZuMDB4aTNqbDd6MGMzMWdkMSJ9.XBV2-35xRf4THAvlcFMOcQ",
}
).addTo(map);
// Apply UI elements
info.addTo(map);
legend.addTo(map);
// Apply map data
geoJson = L.geoJson(globeData, {
style: style,
onEachFeature: onEachFeature,
}).addTo(map);
});