Skip to main content
Source Link
var map=L.map('map').setView([21.14,79.08],5); 
map.on('click', function(e) {
    l1=e.latlng.lat //get lat long of mouse click
    l2=e.latlng.lng

map.on('mousemove',function(e1){
        var lat=e1.latlng.lat;//get live coordinates of mouse movement
        var lng=e1.latlng.lng;
        map.removeLayer(rectangle)//to remove previous loop rectangle
        rectangle=L.rectangle([[l1,l2],[lat,lng]]).addTo(map);
        console.log(lat, lng)
    })

}) //Here first click anywhere on map, the coordinates will be stored in l1 and l2. Then simply move mouse, you will see rectangle getting stretched as you move so a rectangle will be formed of any shape by user.

map.on('mousemove',function(e1){
    var lat=e1.latlng.lat;//get live coordinates of mouse movement
    var lng=e1.latlng.lng;
    map.removeLayer(rectangle)//to remove previous loop rectangle
    rectangle=L.rectangle([[l1,l2],[lat,lng]]).addTo(map);
    console.log(lat, lng)
})
var map=L.map('map').setView([21.14,79.08],5); 
map.on('click', function(e) {
    l1=e.latlng.lat //get lat long of mouse click
    l2=e.latlng.lng

map.on('mousemove',function(e1){
        var lat=e1.latlng.lat;//get live coordinates of mouse movement
        var lng=e1.latlng.lng;
        map.removeLayer(rectangle)//to remove previous loop rectangle
        rectangle=L.rectangle([[l1,l2],[lat,lng]]).addTo(map);
        console.log(lat, lng)
    })

}) //Here first click anywhere on map, the coordinates will be stored in l1 and l2. Then simply move mouse, you will see rectangle getting stretched as you move so a rectangle will be formed of any shape by user.

Source Link

map.on('mousemove',function(e1){
    var lat=e1.latlng.lat;//get live coordinates of mouse movement
    var lng=e1.latlng.lng;
    map.removeLayer(rectangle)//to remove previous loop rectangle
    rectangle=L.rectangle([[l1,l2],[lat,lng]]).addTo(map);
    console.log(lat, lng)
})