File size: 9,366 Bytes
3af080a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
/*
* L.Control.Coordinates is used for displaying current mouse coordinates on the map.
*/
// credit https://github.com/Low-power/Leaflet.Coordinates/tree/wrap-coordinate-option
// import L from 'leaflet';
L.Control.Coordinates = L.Control.extend({
options: {
position: 'bottomright',
//decimals used if not using DMS or labelFormatter functions
decimals: 4,
//decimalseperator used if not using DMS or labelFormatter functions
decimalSeperator: ".",
//label templates for usage if no labelFormatter function is defined
labelTemplateLat: "Lat: {y}",
labelTemplateLng: "Lng: {x}",
//label formatter functions
labelFormatterLat: undefined,
labelFormatterLng: undefined,
//switch on/off input fields on click
enableUserInput: true,
//use Degree-Minute-Second
useDMS: false,
//should coordinate be wrapped as on earth
wrapCoordinate: true,
//if true lat-lng instead of lng-lat label ordering is used
useLatLngOrder: false,
//if true user given coordinates are centered directly
centerUserCoordinates: false,
//leaflet marker type
markerType: L.marker,
//leaflet marker properties
markerProps: {}
},
onAdd: function(map) {
this._map = map;
var className = 'leaflet-control-coordinates',
container = this._container = L.DomUtil.create('div', className),
options = this.options;
//label containers
this._labelcontainer = L.DomUtil.create("div", "uiElement label", container);
this._label = L.DomUtil.create("span", "labelFirst", this._labelcontainer);
//input containers
this._inputcontainer = L.DomUtil.create("div", "uiElement input uiHidden", container);
var xSpan, ySpan;
if (options.useLatLngOrder) {
ySpan = L.DomUtil.create("span", "", this._inputcontainer);
this._inputY = this._createInput("inputY", this._inputcontainer);
xSpan = L.DomUtil.create("span", "", this._inputcontainer);
this._inputX = this._createInput("inputX", this._inputcontainer);
} else {
xSpan = L.DomUtil.create("span", "", this._inputcontainer);
this._inputX = this._createInput("inputX", this._inputcontainer);
ySpan = L.DomUtil.create("span", "", this._inputcontainer);
this._inputY = this._createInput("inputY", this._inputcontainer);
}
xSpan.innerHTML = options.labelTemplateLng.replace("{x}", "");
ySpan.innerHTML = options.labelTemplateLat.replace("{y}", "");
L.DomEvent.on(this._inputX, 'keyup', this._handleKeypress, this);
L.DomEvent.on(this._inputY, 'keyup', this._handleKeypress, this);
//connect to mouseevents
map.on("mousemove", this._update, this);
map.on('dragstart', this.collapse, this);
map.whenReady(this._update, this);
this._showsCoordinates = true;
//wether or not to show inputs on click
if (options.enableUserInput) {
L.DomEvent.addListener(this._container, "click", this._switchUI, this);
}
return container;
},
/**
* Creates an input HTML element in given container with given classname
*/
_createInput: function(classname, container) {
var input = L.DomUtil.create("input", classname, container);
input.type = "text";
L.DomEvent.disableClickPropagation(input);
return input;
},
_clearMarker: function() {
this._map.removeLayer(this._marker);
},
/**
* Called onkeyup of input fields
*/
_handleKeypress: function(e) {
switch (e.keyCode) {
case 27: //Esc
this.collapse();
break;
case 13: //Enter
this._handleSubmit();
this.collapse();
break;
default: //All keys
this._handleSubmit();
break;
}
},
/**
* Called on each keyup except ESC
*/
_handleSubmit: function() {
var x = L.NumberFormatter.createValidNumber(this._inputX.value, this.options.decimalSeperator);
var y = L.NumberFormatter.createValidNumber(this._inputY.value, this.options.decimalSeperator);
if (x !== undefined && y !== undefined) {
var marker = this._marker;
if (!marker) {
marker = this._marker = this._createNewMarker();
marker.on("click", this._clearMarker, this);
}
var ll = new L.LatLng(y, x);
marker.setLatLng(ll);
marker.addTo(this._map);
if (this.options.centerUserCoordinates) {
this._map.setView(ll, this._map.getZoom());
}
}
},
/**
* Shows inputs fields
*/
expand: function() {
this._showsCoordinates = false;
this._map.off("mousemove", this._update, this);
L.DomEvent.addListener(this._container, "mousemove", L.DomEvent.stop);
L.DomEvent.removeListener(this._container, "click", this._switchUI, this);
L.DomUtil.addClass(this._labelcontainer, "uiHidden");
L.DomUtil.removeClass(this._inputcontainer, "uiHidden");
},
/**
* Creates the label according to given options and formatters
*/
_createCoordinateLabel: function(ll) {
var opts = this.options,
x, y;
if (opts.customLabelFcn) {
return opts.customLabelFcn(ll, opts);
}
if (opts.labelFormatterLng) {
x = opts.labelFormatterLng(ll.lng);
} else {
x = L.Util.template(opts.labelTemplateLng, {
x: this._getNumber(ll.lng, opts)
});
}
if (opts.labelFormatterLat) {
y = opts.labelFormatterLat(ll.lat);
} else {
y = L.Util.template(opts.labelTemplateLat, {
y: this._getNumber(ll.lat, opts)
});
}
if (opts.useLatLngOrder) {
return y + " " + x;
}
return x + " " + y;
},
/**
* Returns a Number according to options (DMS or decimal)
*/
_getNumber: function(n, opts) {
var res;
if (opts.useDMS) {
res = L.NumberFormatter.toDMS(n);
} else {
res = L.NumberFormatter.round(n, opts.decimals, opts.decimalSeperator);
}
return res;
},
/**
* Shows coordinate labels after user input has ended. Also
* displays a marker with popup at the last input position.
*/
collapse: function() {
if (!this._showsCoordinates) {
this._map.on("mousemove", this._update, this);
this._showsCoordinates = true;
//var opts = this.options;
L.DomEvent.addListener(this._container, "click", this._switchUI, this);
L.DomEvent.removeListener(this._container, "mousemove", L.DomEvent.stop);
L.DomUtil.addClass(this._inputcontainer, "uiHidden");
L.DomUtil.removeClass(this._labelcontainer, "uiHidden");
if (this._marker) {
var m = this._createNewMarker(),
ll = this._marker.getLatLng();
m.setLatLng(ll);
var container = L.DomUtil.create("div", "");
var label = L.DomUtil.create("div", "", container);
label.innerHTML = this._ordinateLabel(ll);
var close = L.DomUtil.create("a", "", container);
close.innerHTML = "Remove";
close.href = "#";
var stop = L.DomEvent.stopPropagation;
L.DomEvent
.on(close, 'click', stop)
.on(close, 'mousedown', stop)
.on(close, 'dblclick', stop)
.on(close, 'click', L.DomEvent.preventDefault)
.on(close, 'click', function() {
this._map.removeLayer(m);
}, this);
m.bindPopup(container);
m.addTo(this._map);
this._map.removeLayer(this._marker);
this._marker = null;
}
}
},
/**
* Click callback for UI
*/
_switchUI: function(evt) {
L.DomEvent.stop(evt);
L.DomEvent.stopPropagation(evt);
L.DomEvent.preventDefault(evt);
if (this._showsCoordinates) {
//show textfields
this.expand();
} else {
//show coordinates
this.collapse();
}
},
onRemove: function(map) {
map.off("mousemove", this._update, this);
},
/**
* Mousemove callback function updating labels and input elements
*/
_update: function(evt) {
var pos = evt.latlng;
if (pos) {
var opts = this.options;
if(opts.wrapCoordinate || opts.useDMS) {
pos = pos.wrap();
}
this._currentPos = pos;
this._inputY.value = L.NumberFormatter.round(pos.lat, opts.decimals, opts.decimalSeperator);
this._inputX.value = L.NumberFormatter.round(pos.lng, opts.decimals, opts.decimalSeperator);
this._label.innerHTML = this._createCoordinateLabel(pos);
}
},
_createNewMarker: function() {
return this.options.markerType(null, this.options.markerProps);
}
});
//constructor registration
L.control.coordinates = function(options) {
return new L.Control.Coordinates(options);
};
//map init hook
L.Map.mergeOptions({
coordinateControl: false
});
L.Map.addInitHook(function() {
if (this.options.coordinateControl) {
this.coordinateControl = new L.Control.Coordinates();
this.addControl(this.coordinateControl);
}
});
L.NumberFormatter = {
round: function(num, dec, sep) {
var res = L.Util.formatNum(num, dec) + "",
numbers = res.split(".");
if (numbers[1]) {
var d = dec - numbers[1].length;
for (; d > 0; d--) {
numbers[1] += "0";
}
res = numbers.join(sep || ".");
}
return res;
},
toDMS: function(deg) {
var d = Math.floor(Math.abs(deg));
var minfloat = (Math.abs(deg) - d) * 60;
var m = Math.floor(minfloat);
var secfloat = (minfloat - m) * 60;
var s = Math.round(secfloat);
if (s === 60) {
m++;
s = "00";
}
if (m === 60) {
d++;
m = "00";
}
if (s < 10) {
s = "0" + s;
}
if (m < 10) {
m = "0" + m;
}
var dir = "";
if (deg < 0) {
dir = "-";
}
return ("" + dir + d + "° " + m + "' " + s + "''");
},
createValidNumber: function(num, sep) {
if (num && num.length > 0) {
var numbers = num.split(sep || ".");
try {
var numRes = Number(numbers.join("."));
if (isNaN(numRes)) {
return undefined;
}
return numRes;
} catch (e) {
return undefined;
}
}
return undefined;
}
};
|