-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemo.js
executable file
·39 lines (32 loc) · 1.16 KB
/
demo.js
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
const leaflet = require('leaflet');
const GeoJsonLookup = require('geojson-geometries-lookup');
async function fetchMapAsync() {
const response = await fetch('https://unpkg.com/@geo-maps/earth-seas-10m/map.geo.json');
const data = await response.json();
return data;
}
let landLookup = null;
async function isSea(lat, lng) {
if (landLookup === null) {
const map = await fetchMapAsync();
landLookup = new GeoJsonLookup(map);
}
return landLookup.hasContainers({type: 'Point', coordinates: [lng, lat]});
}
async function showInfo(map, latlng) {
latlng = latlng.wrap();
const output = {
isSea: await isSea(latlng.lat, latlng.lng)
};
const msg = JSON.stringify(output, null, 2).replace(/\r?\n|\r/g, '<br/>').replace(/ /g, ' ');
map.openPopup(msg, latlng);
}
const map = leaflet.map('map').setView([0, 0], 1);
leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
map.on('click', e => {
const latlng = e.latlng;
map.openPopup('Loading... ⌛️', latlng);
setTimeout(() => showInfo(map, latlng), 0);
});