Leafletjs: new marker in first click and update the latlng of marker in second click - Javascript Leaflet

Javascript examples for Leaflet:Marker

Description

Leafletjs: new marker in first click and update the latlng of marker in second click

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css"> 
      <script type="text/javascript" src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script> 
      <style id="compiled-css" type="text/css">

#map {//from ww w .  j a v a  2 s  .co  m
   height: 500px;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map("map").fitBounds([[50.5, 5], [52.5, 6]]);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var currentMarker;
map.on("click", function (event) {
   if (currentMarker) {
        currentMarker._icon.style.transition = "transform 0.3s ease-out";
        currentMarker._shadow.style.transition = "transform 0.3s ease-out";
       currentMarker.setLatLng(event.latlng);
        setTimeout(function () {
            currentMarker._icon.style.transition = null;
            currentMarker._shadow.style.transition = null;
        }, 300);
        return;
    }
    currentMarker = L.marker(event.latlng, {
       draggable: true
    }).addTo(map).on("click", function () {
       event.originalEvent.stopPropagation();
    });
});
document.getElementById("done").addEventListener("click", function () {
   currentMarker = null;
});
    }

      </script> 
   </head> 
   <body> 
      <div id="map"></div> 
      <button id="done">Done moving current marker</button>  
   </body>
</html>

Related Tutorials