<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Realtime location tracker</title>
<!-- leaflet css -->
<link rel="stylesheet" href="https://unpkg.com/
[email protected]/dist/leaflet.css" />
<style>
body {
margin: 0;
padding: 0;
}
#map {
width: 100%;
height: 100vh;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
<!-- leaflet js -->
<script>
// Map initialization
// var map = L.map('map').setView([14.0860746, 100.608406], 6);
var map = L.map('map').setView([4.8117582, 6.9708657], 21.478);
//osm layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker([51.5, -0.09]).addTo(map)
.bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
.openPopup();
if (!navigator.geolocation) {
console.log("Your browser doesn't support geolocation feature!")
} else {
setInterval(() => {
navigator.geolocation.getCurrentPosition(getPosition)
}, 5000);
}
var marker, circle;
function getPosition(position) {
// console.log(position)
var lat = position.coords.latitude
var long = position.coords.longitude
var accuracy = position.coords.accuracy
// if (marker) {
// map.removeLayer(marker)
// }
// if (circle) {
// map.removeLayer(circle)
// }
marker = L.marker([lat, long])
circle = L.circle([lat, long], {radius: accuracy})
var featureGroup = L.featureGroup([marker]).addTo(map)
map.fitBounds(featureGroup.getBounds())
console.log("Your coordinate is: Lat: " + lat + " Long: " + long + " Accuracy: " + accuracy)
}
</script>