Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add coord picker in map #14

Merged
merged 2 commits into from Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/map.js
Expand Up @@ -68,6 +68,48 @@ define(["map/clientlayer", "map/labelslayer",
}
})

var CoordsPickerButton = 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", "coord-picker")
button.textContent = ""

// Click propagation isn't disabled as this causes problems with the
// location picking mode; instead propagation is stopped in onClick().
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 (e) {
L.DomEvent.stopPropagation(e)
this.f(!this.active)
}

})

function mkMarker(dict, iconFunc, router) {
return function (d) {
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
Expand Down Expand Up @@ -152,6 +194,13 @@ define(["map/clientlayer", "map/labelslayer",
})
}

var showCoordsPickerButton = new CoordsPickerButton(function (d) {
if (d)
enableCoords()
else
disableCoords()
})

function saveView() {
savedView = {center: map.getCenter(),
zoom: map.getZoom()}
Expand All @@ -171,6 +220,23 @@ define(["map/clientlayer", "map/labelslayer",
locateUserButton.set(false)
}

function enableCoords() {
map.getContainer().classList.add("pick-coordinates")
map.on("click", showCoordinates)
showCoordsPickerButton.set(true)
}

function disableCoords() {
map.getContainer().classList.remove("pick-coordinates")
map.off("click", showCoordinates)
showCoordsPickerButton.set(false)
}

function showCoordinates(e) {
window.prompt("Koordinaten (Lat, Lng)", e.latlng.lat.toFixed(6) + ", " + e.latlng.lng.toFixed(6))
disableCoords()
}

function locationFound(e) {
if (!userLocation)
userLocation = new LocationMarker(e.latlng).addTo(map)
Expand Down Expand Up @@ -228,6 +294,7 @@ define(["map/clientlayer", "map/labelslayer",
map.on("dragend", saveView)

addButton(locateUserButton)
addButton(showCoordsPickerButton)

addButton(new AddLayerButton(function () {
/*eslint no-alert:0*/
Expand Down
4 changes: 4 additions & 0 deletions scss/_map.scss
Expand Up @@ -2,6 +2,10 @@
paint-order: stroke;
}

.pick-coordinates {
cursor: crosshair;
}

.map {
width: 100%;
height: 100%;
Expand Down