-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_country_map.htm
316 lines (314 loc) · 10.2 KB
/
dynamic_country_map.htm
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Country Map</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<style>
#map {
height: 600px;
}
body {
font-family: sans-serif;
}
form {
padding: 1em;
}
</style>
</head>
<body>
<form id="country-form">
<input list="country-names" id="country-input" placeholder="Enter country name">
<datalist id="country-names"></datalist>
<button type="button" id="add-country-btn">Add Country</button>
</form>
<div id="map"></div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-ajax/dist/leaflet.ajax.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const defaultCountries = ['USA', 'CHN', 'FRA', 'RUS', 'GBR'];
const countries = {
'AFG': 'Afghanistan',
'ALB': 'Albania',
'DZA': 'Algeria',
'AND': 'Andorra',
'AGO': 'Angola',
'ATG': 'Antigua and Barbuda',
'ARG': 'Argentina',
'ARM': 'Armenia',
'AUS': 'Australia',
'AUT': 'Austria',
'AZE': 'Azerbaijan',
'BHS': 'Bahamas',
'BHR': 'Bahrain',
'BGD': 'Bangladesh',
'BRB': 'Barbados',
'BLR': 'Belarus',
'BEL': 'Belgium',
'BLZ': 'Belize',
'BEN': 'Benin',
'BTN': 'Bhutan',
'BOL': 'Bolivia',
'BIH': 'Bosnia and Herzegovina',
'BWA': 'Botswana',
'BRA': 'Brazil',
'BRN': 'Brunei',
'BGR': 'Bulgaria',
'BFA': 'Burkina Faso',
'BDI': 'Burundi',
'CPV': 'Cabo Verde',
'KHM': 'Cambodia',
'CMR': 'Cameroon',
'CAN': 'Canada',
'CAF': 'Central African Republic',
'TCD': 'Chad',
'CHL': 'Chile',
'CHN': 'China',
'COL': 'Colombia',
'COM': 'Comoros',
'COG': 'Congo',
'COD': 'Congo (Democratic Republic)',
'CRI': 'Costa Rica',
'CIV': 'Côte d’Ivoire',
'HRV': 'Croatia',
'CUB': 'Cuba',
'CYP': 'Cyprus',
'CZE': 'Czech Republic',
'DNK': 'Denmark',
'DJI': 'Djibouti',
'DMA': 'Dominica',
'DOM': 'Dominican Republic',
'ECU': 'Ecuador',
'EGY': 'Egypt',
'SLV': 'El Salvador',
'GNQ': 'Equatorial Guinea',
'ERI': 'Eritrea',
'EST': 'Estonia',
'SWZ': 'Eswatini',
'ETH': 'Ethiopia',
'FJI': 'Fiji',
'FIN': 'Finland',
'FRA': 'France',
'GAB': 'Gabon',
'GMB': 'Gambia',
'GEO': 'Georgia',
'DEU': 'Germany',
'GHA': 'Ghana',
'GRC': 'Greece',
'GRD': 'Grenada',
'GTM': 'Guatemala',
'GIN': 'Guinea',
'GNB': 'Guinea-Bissau',
'GUY': 'Guyana',
'HTI': 'Haiti',
'HND': 'Honduras',
'HUN': 'Hungary',
'ISL': 'Iceland',
'IND': 'India',
'IDN': 'Indonesia',
'IRN': 'Iran',
'IRQ': 'Iraq',
'IRL': 'Ireland',
'ISR': 'Israel',
'ITA': 'Italy',
'JAM': 'Jamaica',
'JPN': 'Japan',
'JOR': 'Jordan',
'KAZ': 'Kazakhstan',
'KEN': 'Kenya',
'KIR': 'Kiribati',
'PRK': 'Korea (North)',
'KOR': 'Korea (South)',
'KWT': 'Kuwait',
'KGZ': 'Kyrgyzstan',
'LAO': 'Laos',
'LVA': 'Latvia',
'LBN': 'Lebanon',
'LSO': 'Lesotho',
'LBR': 'Liberia',
'LBY': 'Libya',
'LIE': 'Liechtenstein',
'LTU': 'Lithuania',
'LUX': 'Luxembourg',
'MDG': 'Madagascar',
'MWI': 'Malawi',
'MYS': 'Malaysia',
'MDV': 'Maldives',
'MLI': 'Mali',
'MLT': 'Malta',
'MHL': 'Marshall Islands',
'MRT': 'Mauritania',
'MUS': 'Mauritius',
'MEX': 'Mexico',
'FSM': 'Micronesia',
'MDA': 'Moldova',
'MCO': 'Monaco',
'MNG': 'Mongolia',
'MNE': 'Montenegro',
'MAR': 'Morocco',
'MOZ': 'Mozambique',
'MMR': 'Myanmar',
'NAM': 'Namibia',
'NRU': 'Nauru',
'NPL': 'Nepal',
'NLD': 'Netherlands',
'NZL': 'New Zealand',
'NIC': 'Nicaragua',
'NER': 'Niger',
'NGA': 'Nigeria',
'MKD': 'North Macedonia',
'NOR': 'Norway',
'OMN': 'Oman',
'PAK': 'Pakistan',
'PLW': 'Palau',
'PAN': 'Panama',
'PNG': 'Papua New Guinea',
'PRY': 'Paraguay',
'PER': 'Peru',
'PHL': 'Philippines',
'POL': 'Poland',
'PRT': 'Portugal',
'QAT': 'Qatar',
'ROU': 'Romania',
'RUS': 'Russia',
'RWA': 'Rwanda',
'KNA': 'Saint Kitts and Nevis',
'LCA': 'Saint Lucia',
'VCT': 'Saint Vincent and the Grenadines',
'WSM': 'Samoa',
'SMR': 'San Marino',
'STP': 'Sao Tome and Principe',
'SAU': 'Saudi Arabia',
'SEN': 'Senegal',
'SRB': 'Serbia',
'SYC': 'Seychelles',
'SLE': 'Sierra Leone',
'SGP': 'Singapore',
'SVK': 'Slovakia',
'SVN': 'Slovenia',
'SLB': 'Solomon Islands',
'SOM': 'Somalia',
'ZAF': 'South Africa',
'SSD': 'South Sudan',
'ESP': 'Spain',
'LKA': 'Sri Lanka',
'SDN': 'Sudan',
'SUR': 'Suriname',
'SWE': 'Sweden',
'CHE': 'Switzerland',
'SYR': 'Syria',
'TWN': 'Taiwan',
'TJK': 'Tajikistan',
'TZA': 'Tanzania',
'THA': 'Thailand',
'TLS': 'Timor-Leste',
'TGO': 'Togo',
'TON': 'Tonga',
'TTO': 'Trinidad and Tobago',
'TUN': 'Tunisia',
'TUR': 'Turkey',
'TKM': 'Turkmenistan',
'TUV': 'Tuvalu',
'UGA': 'Uganda',
'UKR': 'Ukraine',
'ARE': 'United Arab Emirates',
'GBR': 'United Kingdom',
'USA': 'United States',
'URY': 'Uruguay',
'UZB': 'Uzbekistan',
'VUT': 'Vanuatu',
'VEN': 'Venezuela',
'VNM': 'Vietnam',
'YEM': 'Yemen',
'ZMB': 'Zambia',
'ZWE': 'Zimbabwe'
};
const datalist = document.getElementById('country-names');
Object.values(countries).forEach(name => {
const option = document.createElement('option');
option.value = name;
datalist.appendChild(option);
});
const map = L.map('map').setView([0, 0], 1.4);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map tiles © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors. Thanks to <a href="https://www.naturalearthdata.com/about/contributors/">the Natural Earth team</a> for their public domain maps, and <a href="https://github.com/AshKyd/geojson-regions">AshKyd who made the country outlines</a> from them.',
noWrap: true
}).addTo(map);
const baseUrl = 'https://raw.githubusercontent.com/AshKyd/geojson-regions/main/public/countries/50m/';
const style = feature => ({
color: "gray",
weight: 2,
opacity: 0.3,
fillColor: "gray",
fillOpacity: 0.5
});
const updateMap = () => {
map.eachLayer(layer => {
if (layer.toGeoJSON) map.removeLayer(layer);
});
const bounds = L.latLngBounds(L.latLng(-90, -180), L.latLng(90, 180));
map.setMaxBounds(bounds);
const selectedCountries = [...document.querySelectorAll('input[name="country"]:checked')].map(checkbox => checkbox.value);
selectedCountries.forEach(isoCode => {
const geoJsonFilename = `${isoCode}.geojson`;
const geojsonLayer = new L.GeoJSON.AJAX(`${baseUrl}${geoJsonFilename}`, {
style
}).addTo(map);
});
};
const addCountry = (event, code = null) => {
event.preventDefault();
const countryInput = code || document.getElementById('country-input').value.trim().toUpperCase();
const countryCode = countryInput.length === 3 ? countryInput : Object.keys(countries).find(code => countries[code].toUpperCase() === countryInput);
if (countryCode && countries[countryCode]) {
const existingCheckbox = document.querySelector(`input[value="${countryCode}"]`);
if (existingCheckbox) {
existingCheckbox.checked = true; // Ensure the checkbox is checked
updateMap();
} else {
const form = document.getElementById('country-form');
const newLabel = document.createElement('label');
const newCheckbox = document.createElement('input');
newCheckbox.type = 'checkbox';
newCheckbox.name = 'country';
newCheckbox.value = countryCode;
newCheckbox.checked = true;
newCheckbox.addEventListener('change', updateMap);
newLabel.appendChild(newCheckbox);
newLabel.appendChild(document.createTextNode(' ' + countries[countryCode]));
form.insertBefore(newLabel, form.lastElementChild);
sortCountries();
document.getElementById('country-input').value = '';
updateMap();
}
}
};
const sortCountries = () => {
const form = document.getElementById('country-form');
const labels = Array.from(form.querySelectorAll('label'));
labels.sort((a, b) => a.textContent.localeCompare(b.textContent));
labels.forEach(label => form.appendChild(label));
};
document.querySelectorAll('input[name="country"]').forEach(checkbox => {
checkbox.addEventListener('change', updateMap);
});
// Prevent form submission when pressing enter
document.getElementById('country-form').addEventListener('submit', addCountry);
document.getElementById('country-input').addEventListener('input', (event) => {
if (event.inputType === 'insertReplacementText') {
addCountry(event);
}
});
// Add default countries
defaultCountries.forEach(code => {
addCountry({
preventDefault: () => {}
}, code);
});
updateMap();
});
</script>
</body>
</html>