-
Notifications
You must be signed in to change notification settings - Fork 15
/
Control.Coordinates.js
80 lines (66 loc) · 2.49 KB
/
Control.Coordinates.js
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
"use strict";
/**
* author Michal Zimmermann <[email protected]>
* Displays coordinates of mouseclick.
* @param object options:
* position: bottomleft, bottomright etc. (just as you are used to it with Leaflet)
* latitudeText: description of latitude value (defaults to lat.)
* longitudeText: description of latitude value (defaults to lon.)
* promptText: text displayed when user clicks the control
* precision: number of decimals to be displayed
*/
L.Control.Coordinates = L.Control.extend({
options: {
position: 'bottomleft',
latitudeText: 'lat.',
longitudeText: 'lon.',
promptText: 'Press Ctrl+C to copy coordinates',
precision: 4
},
initialize: function(options)
{
L.Control.prototype.initialize.call(this, options);
},
onAdd: function(map)
{
var className = 'leaflet-control-coordinates',
that = this,
container = this._container = L.DomUtil.create('div', className);
this.visible = false;
L.DomUtil.addClass(container, 'hidden');
L.DomEvent.disableClickPropagation(container);
this._addText(container, map);
L.DomEvent.addListener(container, 'click', function() {
var lat = L.DomUtil.get(that._lat),
lng = L.DomUtil.get(that._lng),
latTextLen = this.options.latitudeText.length + 1,
lngTextLen = this.options.longitudeText.length + 1,
latTextIndex = lat.textContent.indexOf(this.options.latitudeText) + latTextLen,
lngTextIndex = lng.textContent.indexOf(this.options.longitudeText) + lngTextLen,
latCoordinate = lat.textContent.substr(latTextIndex),
lngCoordinate = lng.textContent.substr(lngTextIndex);
window.prompt(this.options.promptText, latCoordinate + ' ' + lngCoordinate);
}, this);
return container;
},
_addText: function(container, context)
{
this._lat = L.DomUtil.create('span', 'leaflet-control-coordinates-lat' , container),
this._lng = L.DomUtil.create('span', 'leaflet-control-coordinates-lng' , container);
return container;
},
/**
* This method should be called when user clicks the map.
* @param event object
*/
setCoordinates: function(obj)
{
if (!this.visible) {
L.DomUtil.removeClass(this._container, 'hidden');
}
if (obj.latlng) {
L.DomUtil.get(this._lat).innerHTML = '<strong>' + this.options.latitudeText + ':</strong> ' + obj.latlng.lat.toFixed(this.options.precision).toString();
L.DomUtil.get(this._lng).innerHTML = '<strong>' + this.options.longitudeText + ':</strong> ' + obj.latlng.lng.toFixed(this.options.precision).toString();
}
}
});