Browse Source

Add location picker

Michael Schwarz 8 years ago
parent
commit
9f16adacb1
2 changed files with 72 additions and 0 deletions
  1. 64 0
      lib/map.js
  2. 8 0
      scss/_map.scss

+ 64 - 0
lib/map.js

@@ -29,6 +29,45 @@ define(["map/clientlayer", "map/labelslayer",
         }
     })
 
+    var CoordsButton = L.Control.extend({
+      options: {
+        position: "bottomright"
+      },
+
+      active: false,
+      button: undefined,
+
+      initialize: function (f, options) {
+        L.Util.setOptions(this, options)
+        this.f = f
+      },
+
+      onAdd: function () {
+        var button = L.DomUtil.create("button", "show-coords")
+
+        L.DomEvent.disableClickPropagation(button)
+        L.DomEvent.addListener(button, "click", this.onClick, this)
+
+        this.button = button
+
+        return button
+      },
+
+      update: function() {
+        this.button.classList.toggle("active", this.active)
+      },
+
+      set: function(v) {
+        this.active = v
+        this.update()
+      },
+
+      onClick: function () {
+        this.f(!this.active)
+      }
+
+    })
+
     var LocateButton = L.Control.extend({
         options: {
           position: "bottomright"
@@ -152,6 +191,13 @@ define(["map/clientlayer", "map/labelslayer",
         })
       }
 
+      var showCoordsButton = new CoordsButton(function (d) {
+        if (d)
+          enableCoords()
+        else
+          disableCoords()
+      })
+
       function saveView() {
         savedView = {center: map.getCenter(),
                      zoom: map.getZoom()}
@@ -171,6 +217,18 @@ define(["map/clientlayer", "map/labelslayer",
         locateUserButton.set(false)
       }
 
+      function enableCoords() {
+        map.getContainer().classList.add("pick-coordinates")
+        map.on("click", showCoordinates)
+        showCoordsButton.set(true)
+      }
+
+      function disableCoords() {
+        map.getContainer().classList.remove("pick-coordinates")
+        map.off("click", showCoordinates)
+        showCoordsButton.set(false)
+      }
+
       function locationFound(e) {
         if (!userLocation)
           userLocation = new LocationMarker(e.latlng).addTo(map)
@@ -179,6 +237,11 @@ define(["map/clientlayer", "map/labelslayer",
         userLocation.setAccuracy(e.accuracy)
       }
 
+      function showCoordinates(e) {
+        window.alert("Koordinaten: " + e.latlng.lat.toFixed(6) + ", " + e.latlng.lng.toFixed(6))
+        disableCoords()
+      }
+
       function locationError() {
         if (userLocation) {
           map.removeLayer(userLocation)
@@ -228,6 +291,7 @@ define(["map/clientlayer", "map/labelslayer",
       map.on("dragend", saveView)
 
       addButton(locateUserButton)
+      addButton(showCoordsButton)
 
       addButton(new AddLayerButton(function () {
         /*eslint no-alert:0*/

+ 8 - 0
scss/_map.scss

@@ -2,6 +2,14 @@
   paint-order: stroke;
 }
 
+.pick-coordinates {
+  cursor: crosshair;
+}
+
+button.show-coords:after {
+  content: '\f2a6';
+}
+
 .map {
   width: 100%;
   height: 100%;