]> git.openstreetmap.org Git - rails.git/commitdiff
Allow context menu to add notes without panning the map
authorTom Hughes <tom@compton.nu>
Sun, 12 Feb 2017 17:26:17 +0000 (17:26 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 12 Feb 2017 19:19:14 +0000 (19:19 +0000)
app/assets/javascripts/index/contextmenu.js
app/assets/javascripts/index/new_note.js

index 8a4654b97b9316b336e1ff70e8f031e9273aed71..1e7251ec62a99b6d3f81f749d183dc28ac10de0a 100644 (file)
@@ -30,9 +30,12 @@ OSM.initializeContextMenu = function (map) {
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.add_note"),
     callback: function addNoteHere(e) {
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.add_note"),
     callback: function addNoteHere(e) {
-      // I'd like this, instead of panning, to pass a query parameter about where to place the marker
-      map.panTo(e.latlng.wrap(), {animate: false});
-      OSM.router.route("/note/new");
+      var precision = OSM.zoomPrecision(map.getZoom()),
+          latlng = e.latlng.wrap(),
+          lat = latlng.lat.toFixed(precision),
+          lng = latlng.lng.toFixed(precision);
+
+      OSM.router.route("/note/new?lat=" + lat + "&lon=" + lng);
     }
   });
 
     }
   });
 
index 397daa637f4f7c2397b584a45339cda5a15d6231..53697e65b584d20686d3ee9bf3d200878fb9fac2 100644 (file)
@@ -77,7 +77,9 @@ OSM.NewNote = function(map) {
   }
 
   page.pushstate = page.popstate = function (path) {
   }
 
   page.pushstate = page.popstate = function (path) {
-    OSM.loadSidebarContent(path, page.load);
+    OSM.loadSidebarContent(path, function () {
+      page.load(path);
+    });
   };
 
   function newHalo(loc, a) {
   };
 
   function newHalo(loc, a) {
@@ -97,7 +99,7 @@ OSM.NewNote = function(map) {
     }
   }
 
     }
   }
 
-  page.load = function () {
+  page.load = function (path) {
     if (addNoteButton.hasClass("disabled")) return;
     if (addNoteButton.hasClass("active")) return;
 
     if (addNoteButton.hasClass("disabled")) return;
     if (addNoteButton.hasClass("active")) return;
 
@@ -105,12 +107,34 @@ OSM.NewNote = function(map) {
 
     map.addLayer(noteLayer);
 
 
     map.addLayer(noteLayer);
 
-    var mapSize = map.getSize();
-    var markerPosition;
+    var params = querystring.parse(path.substring(path.indexOf('?') + 1));
+    var markerLatlng;
+
+    if (params.lat && params.lon) {
+      markerLatlng = L.latLng(params.lat, params.lon);
+
+      var markerPosition = map.latLngToContainerPoint(markerLatlng),
+          mapSize = map.getSize(),
+          panBy = L.point(0, 0);
+
+      if (markerPosition.x < 50) {
+        panBy.x = markerPosition.x - 50;
+      } else if (markerPosition.x > mapSize.x - 50) {
+        panBy.x = 50 - mapSize.x + markerPosition.x;
+      }
 
 
-    markerPosition = [mapSize.x / 2, mapSize.y / 2];
+      if (markerPosition.y < 50) {
+        panBy.y = markerPosition.y - 50;
+      } else if (markerPosition.y > mapSize.y - 50) {
+        panBy.y = 50 - mapSize.y + markerPosition.y;
+      }
+
+      map.panBy(panBy);
+    } else {
+      markerLatlng = map.getCenter();
+    }
 
 
-    newNote = L.marker(map.containerPointToLatLng(markerPosition), {
+    newNote = L.marker(markerLatlng, {
       icon: noteIcons["new"],
       opacity: 0.9,
       draggable: true
       icon: noteIcons["new"],
       opacity: 0.9,
       draggable: true