Browse Source

Fix issue #12 : marker is not present on map by default

User has to click on the map to create it

Signed-off-by: CapsLock <faimaison@legeox.net>
CapsLock 9 years ago
parent
commit
0eebb8e3c6
1 changed files with 22 additions and 13 deletions
  1. 22 13
      wifiwithme/static/form.js

+ 22 - 13
wifiwithme/static/form.js

@@ -22,22 +22,31 @@ $( document ).ready(function() {
         maxWidth: 150
     }).addTo(map);
 
-    // Create marker
-    var marker = L.marker([defaults.lat,defaults.lng], {
-        draggable: true
-    }).addTo(map);
+    var marker = null;
+
+    map.on('click', (e) => {
+        // Create marker
+        if (marker === null) {
+            marker = L.marker([defaults.lat, defaults.lng], {
+                draggable: true
+            }).addTo(map);
+        }
 
-    // every time the marker is dragged, update the coordinates container
-    marker.on('dragend', mapUpdateCoords);
+        // every time the marker is dragged, update the coordinates container
+        marker.on('dragend', mapUpdateCoords);
 
-    // Set the initial marker coordinate on load.
-    mapUpdateCoords();
+        marker.setLatLng(e.latlng)
 
-    function mapUpdateCoords() {
-        var m = marker.getLatLng();
-        $('#id_latitude').val(m.lat);
-        $('#id_longitude').val(m.lng);
-    }
+        mapUpdateCoords()
+
+        // every time the marker is dragged, update the coordinates container
+        marker.on('dragend', mapUpdateCoords);
+        function mapUpdateCoords() {
+            var m = marker.getLatLng();
+            $('#id_latitude').val(m.lat);
+            $('#id_longitude').val(m.lng);
+        }
+    });
 
     // Display tiny circles on existing public points
     var GeoJsonPath = $('#map').data('json');