user6889084
user6889084

Reputation:

Add Marker on click & get lat long differently

Code for popup onMouseClick, which shows lat & long

var popup = L.popup();

    function onMapClick(e) {
        popup
            .setLatLng(e.latlng)
            .setContent("You clicked the map at " + e.latlng.toString())
            .openOn(mymap);
    }

    mymap.on('click', onMapClick);

Is it possible to add marker, i tried to put 'marker in function, but didn't work.

And how to get lat long differently, so i can copy it in two diff variables

Fiddle here

Upvotes: 0

Views: 1923

Answers (2)

Akshay
Akshay

Reputation: 805

i think you are expecting something like this

function onMapClick(e) {
 new L.marker([e.latlng.lat,e.latlng.lng]).addTo(map).bindPopup("You clicked the map at " + e.latlng.toString()).openPopup();
    }

Upvotes: 0

Weedoze
Weedoze

Reputation: 13953

You can add the marker directly inside your onclick function

var newMarker = new L.marker(e.latlng).addTo(mymap);

And to get the lat and lng separately you were close

var lat = e.latlng.lat;
var lng = e.latlng.lng;

Finally, here is your updated JSFiddle

Upvotes: 0

Related Questions