-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathInputfieldLeafletMapMarker.js
151 lines (122 loc) · 5.43 KB
/
InputfieldLeafletMapMarker.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
/**
* Display a Leaflet Map and pinpoint a location for InputfieldLeafletMapMarker
*
*/
var InputfieldLeafletMapMarker = {
options: {
zoom: 9,
draggable: true,
center: null
},
init: function(mapId, lat, lng, zoom, mapType, provider) {
var mapElement = document.getElementById(mapId);
var options = InputfieldLeafletMapMarker.options;
if(zoom < 1) zoom = 9;
//options.center = new google.maps.LatLng(lat, lng);
options.zoom = parseInt(zoom);
var map = L.map(mapElement).setView([lat, lng], options.zoom);
L.tileLayer.provider(provider).addTo(map);
var coder = L.Control.Geocoder.nominatim(),
geocoder = L.Control.geocoder({
geocoder: geocoder, placeholder: ''
}).addTo(map);
var marker = L.marker(
[lat,lng],
{draggable: options.draggable}
).addTo(map);
var $map = $('#' + mapId);
//var $latlng = $map.siblings(".InputfieldLeafletMapMarkerLatLng").find("input[type=text]");
var $lat = $map.siblings(".InputfieldLeafletMapMarkerLat").find("input[type=text]");
var $lng = $map.siblings(".InputfieldLeafletMapMarkerLng").find("input[type=text]");
var $addr = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[type=text]");
var $addrJS = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[type=hidden]");
var $raw = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[name$=_raw]");
var $toggle = $map.siblings(".InputfieldLeafletMapMarkerToggle").find("input[type=checkbox]");
var $zoom = $map.siblings(".InputfieldLeafletMapMarkerZoom").find("input[type=number]");
var $notes = $map.siblings(".notes");
var $latlng = '';
$( ".InputfieldLeafletMapMarkerAddress" ).on( "click", function() {
$(".leaflet-control-geocoder.leaflet-control").toggleClass("leaflet-control-geocoder-expanded");
setTimeout(function() { $('input.undefined').focus() }, 300);
});
$lat.val(marker.getLatLng().lat);
$lng.val(marker.getLatLng().lng);
$zoom.val(map.getZoom());
$zoom.change(function(event) {
map.setZoom($zoom.val());
map.setView(marker.getLatLng());
});
$lat.change(function(event) {
marker.setLatLng([$lat.val(),$lng.val()]);
map.setView(marker.getLatLng(), 9);
coder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) {
var r = results[0];
if (r) {
$addr.val(r.name);
$raw.val(JSON.stringify(r.properties));
}
})
});
$lng.change(function(event) {
marker.setLatLng([$lat.val(),$lng.val()]);
map.setView(marker.getLatLng(), 9);
coder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) {
var r = results[0];
if (r) {
$addr.val(r.name);
$raw.val(JSON.stringify(r.properties));
}
})
});
geocoder.markGeocode = function(result) {
marker.setLatLng(result.center);
map.fitBounds(result.bbox);
$lat.val(result.center.lat);
$lng.val(result.center.lng);
$addr.val(result.name);
$raw.val(JSON.stringify(result.properties));
};
marker.on('dragend', function(event) {
var result = marker.getLatLng();
$lat.val(result.lat).trigger('change.custom');
$lng.val(result.lng).trigger('change.custom');
//reverse geocoding displays in the adress field
coder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) {
var r = results[0];
if (r) {
$addr.val(r.name);
$raw.val(JSON.stringify(r.properties));
}
})
});
map.on('zoomend', function(event){
$zoom.val(map.getZoom());
});
// get the tab element where this map is integrated
var $map = $('#' + mapId);
// This seem to no longer work with Uikit theme.
// var $tab = $('#_' + $map.closest('.InputfieldFieldsetTabOpen').attr('id'));
// // get the inputfield where this map is integrated and add the tab to the stack
// var $inputFields = $map.closest('.Inputfield').find('.InputfieldStateToggle').add($tab);
// Get closed wrappers around the map.
var $inputFields = $map.parents('.Inputfield.InputfieldStateCollapsed');
// Refresh the map when any of the wrappers open.
$inputFields.on('opened',function(){
window.setTimeout(function(){
map.invalidateSize();
}, 200);
});
}
};
function initializeLeafletMap() {
$(".InputfieldLeafletMapMarkerMap").each(function(item) {
var $t = $(this);
if (!$t.children().length) {
InputfieldLeafletMapMarker.init($t.attr('id'), $t.attr('data-lat'), $t.attr('data-lng'), $t.attr('data-zoom'), $t.attr('data-type'), $t.attr('data-provider'));
}
});
}
$(document).ready(function() {
initializeLeafletMap();
$(document).on('reloaded', '.InputfieldLeafletMapMarker', initializeLeafletMap);
});