Browse Source

Fix issue #12 : marker is placed on map by default

Marker is back on map by default (user does not need to click on the map
anymore)

Fields lat / lon are updated _only_ if marker has been moved which
prevents submitting form with default coordinates

Signed-off-by: CapsLock <faimaison@legeox.net>
CapsLock 9 years ago
parent
commit
e9cbe31d17
2 changed files with 13 additions and 21 deletions
  1. 1 1
      wifiwithme/apps/contribmap/forms.py
  2. 12 20
      wifiwithme/static/form.js

+ 1 - 1
wifiwithme/apps/contribmap/forms.py

@@ -67,4 +67,4 @@ class PublicContribForm(forms.ModelForm):
         super(PublicContribForm, self).__init__(*args, **kwargs)
 
         for f in ['latitude', 'longitude']:
-            self.fields[f].error_messages['required'] = "Veuillez sélectionner un point sur la carte"
+            self.fields[f].error_messages['required'] = "Veuillez déplacer le curseur à l'endroit où vous voulez partager/accéder au service"

+ 12 - 20
wifiwithme/static/form.js

@@ -22,32 +22,24 @@ $( document ).ready(function() {
         maxWidth: 150
     }).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);
-        }
+    var 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);
 
+    map.on('click', (e) => {
         marker.setLatLng(e.latlng)
-
         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);
-        }
     });
 
+    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');
     $.getJSON(GeoJsonPath, function(data){