-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathindex.html
More file actions
79 lines (70 loc) · 2.13 KB
/
index.html
File metadata and controls
79 lines (70 loc) · 2.13 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
<!doctype html>
<html>
<head>
<title>Leaflet Control Geocoder</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no initial-scale=1, maximum-scale=1"
/>
<link rel="stylesheet" href="node_modules/leaflet/dist/leaflet.css" />
<link rel="stylesheet" href="src/style.css" />
<style>
body {
margin: 0;
}
#map {
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as L from 'leaflet';
import { Geocoder } from './src/index.ts';
const map = new L.Map('map').setView([0, 0], 2);
let geocoder = Geocoder.nominatim();
// parse /?geocoder=nominatim from URL
let params = new URL(document.location.toString()).searchParams;
const geocoderString = params.get('geocoder');
if (geocoderString && Geocoder[geocoderString]) {
console.log('Using geocoder', geocoderString);
geocoder = Geocoder[geocoderString]();
} else if (geocoderString) {
console.warn('Unsupported geocoder', geocoderString);
}
const control = new Geocoder({
query: 'Moon',
placeholder: 'Search here...',
geocoder
}).addTo(map);
setTimeout(() => {
control.setQuery('Earth');
}, 12000);
new L.TileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let marker;
map.on('click', e => {
geocoder.reverse(e.latlng, map.options.crs.scale(map.getZoom())).then(results => {
const r = results[0];
if (!r) {
return;
}
if (marker) {
marker
.setLatLng(r.center)
.setPopupContent(r.html || r.name)
.openPopup();
} else {
marker = new L.Marker(r.center).bindPopup(r.name).addTo(map).openPopup();
}
});
});
</script>
</body>
</html>