forked from FriendsOfProcessWire/FieldtypeLeafletMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputfieldLeafletMapMarker.js
177 lines (144 loc) · 5.99 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
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
/**
* 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 options = InputfieldLeafletMapMarker.options;
if(zoom < 1) zoom = 9;
//options.center = new google.maps.LatLng(lat, lng);
options.zoom = parseInt(zoom);
var map = L.map(document.getElementById(mapId)). 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 $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);
}
})
});
$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);
}
})
});
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);
};
marker.on('dragend', function(event) {
var result = marker.getLatLng();
$lat.val(result.lat);
$lng.val(result.lng);
//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);
}
})
});
map.on('zoomend', function(event){
$zoom.val(map.getZoom());
})
/*
google.maps.event.addListener(map, 'zoom_changed', function() {
$zoom.val(map.getZoom());
});
$addr.blur(function() {
if(!$toggle.is(":checked")) return true;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': $(this).val()}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK && results[0]) {
var position = results[0].geometry.location;
map.setCenter(position);
marker.setPosition(position);
$lat.val(position.lat());
$lng.val(position.lng());
$addrJS.val($addr.val());
}
$notes.text(status);
});
return true;
});
$zoom.change(function() {
map.setZoom(parseInt($(this).val()));
});
$toggle.click(function() {
if($(this).is(":checked")) {
$notes.text('Geocode ON');
// google.maps.event.trigger(marker, 'dragend');
$addr.trigger('blur');
} else {
$notes.text('Geocode OFF');
}
return true;
});
// added by diogo to solve the problem of maps not rendering correctly in hidden elements
// trigger a resize on the map when either the tab button or the toggle field bar are pressed
// get the tab element where this map is integrated
var $map = $('#' + mapId);
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);
$inputFields.on('click',function(){
// give it time to open
window.setTimeout(function(){
google.maps.event.trigger(map,'resize');
map.setCenter(options.center);
}, 200);
});
*/
}
};
$(document).ready(function() {
$(".InputfieldLeafletMapMarkerMap").each(function() {
var $t = $(this);
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'));
});
});