Leaflet get latitude longitude

Bagaimana cara mengetahui letak titik koordinat Latitude dan longitude peta pada leaflet, seperti pada gambar dibawah. jadi ketika di klik pada peta akan muncul alert titik koordinat.

leaflet-library-css-js-4

Berikut contoh kode sederhana untuk mengambil titik koordinat tersebut.

function main() {

var options = {
center: [0.5769167874644966, 123.01858018012717],
zoom: 15
}

var map = L.map('map', options);

L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
{attribution: 'CARTO'})
.addTo(map);

// get coordinate
map.on('click',
function(e){
var coord = e.latlng.toString().split(',');
var lat = coord[0].split('(');
var lng = coord[1].split(')');
alert("You clicked the map at latitude: " + lat[1] + " and longitude:" + lng[0]);
});
}
window.onload = main;

Semoga bermanfaat. wassalam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s