-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosmpicker.js
165 lines (148 loc) · 4.63 KB
/
osmpicker.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
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
/**
* @summary GPS coordinate picker with OpenStreetMap
*
* Turns an input element to a GPS coordinate picker using the default OpenStreetMap layer.
*
* @access public
*
* @class
*
* @param {Object} settings - Configuration.
* @param {string} settings.selector - The HTML element ID of the input element.
* @param {string} settings.inputElement - The HTML element of the input element.
* @param {string} settings.mapElement - The HTML element of the map element.
* @param {Object} settings.defaultView - The default GPS and zoom the map should show when there is nothing else to show.
* @param {Object|Array} settings.defaultView.latLng - Latitude and longitude coordinates in Leaflet LatLng object or array form.
* @param {Integer} settings.defaultView.zoom - The Leaflet map's zoom level..
*/
function OsmPicker(settings) {
/**
* The input element we will turn into a picker.
*
* @access private
* @property {Object} _inputElement - Input DOM element.
*/
var _inputElement = null;
/**
* The DOM element where we render the Leaflet map.
*
* @access private
* @property {Object} _mapElement - Leaflet map DOM element.
*/
var _mapElement = null;
/**
* The Leaflet map.
*
* @access private
* @property {Object} _map - Leaflet map.
*/
var _map = null;
/**
* The current Leaflet marker on the map.
*
* @access private
* @property {Object} _marker - Leaflet marker.
*/
var _marker = null;
/**
* @summary Process the passed settings object and
* calculate default settings from it then create
* the picker.
*
* @access private
*/
function _initialize() {
// Default settings.
settings = settings || {};
settings.defaultView = settings.defaultView || {};
settings.defaultView.latLng = settings.defaultView.latLng || L.latLng(47.4953, 19.0645);
settings.defaultView.zoom = settings.defaultView.zoom || 14;
// Configure input element.
_inputElement = settings.inputElement || document.getElementById(settings.selector);
// Create map element and leaflet map
if (settings.mapElement) {
_mapElement = settings.mapElement
_map = L.map(_mapElement);
} else {
_mapElement = document.createElement('div');
_mapElement.id = settings.selector + '-osm-picker';
_mapElement.style = 'height:300px;cursor:crosshair;';
_insertAfter(_mapElement, _inputElement);
_map = L.map(settings.selector + '-osm-picker');
}
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(_map);
var inputLatLng = _getLatLngFromInput();
_map.setView(inputLatLng || settings.defaultView.latLng, settings.defaultView.zoom);
// Add default marker if input isn't empty.
if (inputLatLng) {
_setMarker(inputLatLng);
}
// Handle map clicks, input changes (marker placement).
_map.on('click', function (e) {
_inputElement.value = e.latlng.lat + ',' + e.latlng.lng;
_setMarker(e.latlng);
});
_inputElement.addEventListener('change', function () {
_setMarker(_getLatLngFromInput());
});
}
/**
* @summary Return the Leaflet map object used by the picker.
*
* @access public
*
* @return {Object} Leaflet map.
*/
this.getMap = function () {
return _map;
}
/**
* @summary Set the picker to the passed GPS coordinate.
*
* @access public
*
* @param {Object|Array} latLng - Latitude and longitude coordinates in Leaflet LatLng or array format.
*/
this.setInputFromLatLng = function (latLng) {
_inputElement.value = (Array.isArray(latLng)) ? latLng.join(',') : latLng.lat + ',' + latLng.lng;
_map.setView(latLng);
_setMarker(latLng);
}
/**
* @summary Create Leaflet LatLng object from the input value.
*
* @access private
*
* @return {Object} Leaflet LatLng representing the input GPS coordinate.
*/
function _getLatLngFromInput() {
return (_inputElement.value !== '') ? L.latLng.apply(this, _inputElement.value.split(',')) : null;
}
/**
* @summary Set the picker's marker to the passed GPS coordinate.
*
* @access private
*
* @param {Object|Array} latLng - Latitude and longitude coordinates in Leaflet LatLng or array format.
*/
function _setMarker(latLng) {
if (_marker !== null) {
_map.removeLayer(_marker);
}
_marker = L.marker(latLng).addTo(_map);
}
/**
* @summary Insert a DOM element after another one.
*
* @access private
*
* @param {Object} newNode - The new DOM element to insert.
* @param {Object} referenceNode - The DOM element after the insertion should happen.
*/
function _insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
_initialize();
}