-
Notifications
You must be signed in to change notification settings - Fork 20
/
leaflet.html
59 lines (50 loc) · 1.9 KB
/
leaflet.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Geolocation con Leaflet</title>
<link rel="stylesheet" href="https://npmcdn.com/[email protected]/dist/leaflet.css" />
<style type="text/css">
#mapa {
width: 100%;
height: 300px;
margin: 0;
}
</style>
</head>
<body>
<div id="mapa"></div>
<script src="https://npmcdn.com/[email protected]/dist/leaflet.js"></script>
<script type="text/javascript">
((() => {
let map;
function init() {
map = new L.Map('mapa');
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
maxZoom: 18
}).addTo(map);
map.attributionControl.setPrefix(''); // Don't show the 'Powered by Leaflet' text.
// map view before we get the location
map.setView(new L.LatLng(51.505, -0.09), 13);
}
function onLocationFound(e) {
const radius = e.accuracy / 2;
const location = e.latlng;
L.marker(location).addTo(map)
L.circle(location, radius).addTo(map);
}
function onLocationError(e) {
console.warn(e.message);
}
function getLocationLeaflet() {
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({setView: true, maxZoom: 16});
}
init()
getLocationLeaflet()
}))();
</script>
</body>
</html>