forked from bellshade/Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolocation.js
37 lines (31 loc) · 1.52 KB
/
geolocation.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
/*eslint-disable no-undef */
let long = document.getElementById("geo-long"); // menyeleksi container longitude
let lat = document.getElementById("geo-lat"); // menyeleksi container latitude
let btn = document.getElementById("geo-btn"); // menyeleksi button
let navStatus = document.getElementById("status"); // menyeleksi container
function show(position) {
const longText = document.createTextNode(position.coords.longitude);
const latText = document.createTextNode(position.coords.latitude);
long.appendChild(longText); // mengisi data longitude
lat.appendChild(latText); // mengisi data latitude
navStatus.innerHTML = "Access Granted";
navStatus.style.color = "green";
let mymap = L.map("geo-map").setView([-5, 120], 4); // initialisasi leaflet map
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
"© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
}).addTo(mymap);
L.marker([position.coords.latitude, position.coords.longitude])
.addTo(mymap) // menandai lokasi dengan marker
.bindPopup(
`<div>lokasi mu disini: <br>latitude: ${position.coords.latitude} <br>longitude: ${position.coords.longitude}</div>`
); // mengisi data di pop-up
}
function error(err) {
navStatus.innerText = `ERROR: ${err.message} (${err.code})`;
navStatus.style.color = "red";
}
function showGeoData() {
navigator.geolocation.getCurrentPosition(show, error);
}
btn.addEventListener("click", showGeoData); // menjalankan showGeoData saat btn diklik