Leaflet js: How to get Lat and lng at the end of drawing any marker on Map RRS feed

  • Question

  • User264732274 posted

    i got a one code which is close but i am not sure because i am first time working with Leaflet js.

    my intention is: suppose Leaflet js is showing map or non geographical data and i have one Leaflet js related toolbar. toolbar has many marker like circle, poly, square etc. when user click on any marker on toolbar to start drawing on map then when drawing of marker will be finish then how do i know?

    how could i attach a function which will work as callback for drawing finish and the function also let me know the Lat and lng of marker. if i know the Lat and lng then i can save in db and later we can reuse it to draw the shape.

    the below code i got

    var map = L.map('mapcanvas').setView([51.505, -0.09], 13);
    map.on('click', function(e){
        // Place marker
        var marker = new L.marker(e.latlng).addTo(map);
        // Ajax query to save the values:
        var data = {
            lat: e.latlng.lat,
            lng: e.latlng.lng
        // saving lat and lng to db by ajax call
        var request = new XMLHttpRequest();
            request.open('POST', '/my/url', true);
            request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

    please have a look at the code and i am not sure does it solve my purpose because here the code work with click event with map.

    so when we click on map then marker drawing would be complete. so please read my post and understand what i am looking for and suggest me with best idea how to achieve it. thanks

    Thursday, March 24, 2016 5:09 AM