-
Notifications
You must be signed in to change notification settings - Fork 0
/
geofield_gmap.js
executable file
·214 lines (193 loc) · 7.81 KB
/
geofield_gmap.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
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
(function ($) {
Backdrop.behaviors.geofieldMapInit = {
attach: function (context, settings) {
// Init all maps in Backdrop.settings.
if (settings.geofield_gmap) {
$.each(settings.geofield_gmap, function(mapid, options) {
geofield_gmap_initialize({
lat: options.lat,
lng: options.lng,
zoom: options.zoom,
latid: options.latid,
lngid: options.lngid,
searchid: options.searchid,
mapid: options.mapid,
widget: options.widget,
map_type: options.map_type,
confirm_center_marker: options.confirm_center_marker,
click_to_place_marker: options.click_to_place_marker,
});
});
}
}
};
})(jQuery);
var geofield_gmap_geocoder;
var geofield_gmap_data = [];
// Center the map to the marker location.
function geofield_gmap_center(mapid) {
google.maps.event.trigger(geofield_gmap_data[mapid].map, 'resize');
geofield_gmap_data[mapid].map.setCenter(geofield_gmap_data[mapid].marker.getPosition());
}
// Place marker at the current center of the map.
function geofield_gmap_marker(mapid) {
if (geofield_gmap_data[mapid].confirm_center_marker) {
if (!window.confirm('Change marker position ?')) return;
}
google.maps.event.trigger(geofield_gmap_data[mapid].map, 'resize');
var position = geofield_gmap_data[mapid].map.getCenter();
geofield_gmap_data[mapid].marker.setPosition(position);
geofield_gmap_data[mapid].lat.val(position.lat());
geofield_gmap_data[mapid].lng.val(position.lng());
if (geofield_gmap_data[mapid].search) {
geofield_gmap_geocoder.geocode({'latLng': position}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
geofield_gmap_data[mapid].search.val(results[0].formatted_address);
}
}
});
}
}
// Init google map.
function geofield_gmap_initialize(params) {
geofield_gmap_data[params.mapid] = params;
jQuery.noConflict();
if (!geofield_gmap_geocoder) {
geofield_gmap_geocoder = new google.maps.Geocoder();
}
var location = new google.maps.LatLng(params.lat, params.lng);
var options = {
zoom: params.zoom,
center: location,
mapTypeId: google.maps.MapTypeId.SATELLITE,
scaleControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
}
};
switch (params.map_type) {
case "ROADMAP":
options.mapTypeId = google.maps.MapTypeId.ROADMAP;
break;
case "SATELLITE":
options.mapTypeId = google.maps.MapTypeId.SATELLITE;
break;
case "HYBRID":
options.mapTypeId = google.maps.MapTypeId.HYBRID;
break;
case "TERRAIN":
options.mapTypeId = google.maps.MapTypeId.TERRAIN;
break;
default:
options.mapTypeId = google.maps.MapTypeId.ROADMAP;
}
var map = new google.maps.Map(document.getElementById(params.mapid), options);
geofield_gmap_data[params.mapid].map = map;
// Fix http://code.google.com/p/gmaps-api-issues/issues/detail?id=1448.
google.maps.event.addListener(map, "idle", function () {
google.maps.event.trigger(map, 'resize');
});
// Fix map issue in fieldgroups / vertical tabs
// https://www.drupal.org/node/2474867.
google.maps.event.addListenerOnce(map, "idle", function () {
// Show all map tiles when a map is shown in a vertical tab.
jQuery('#' + params.mapid).closest('div.vertical-tabs').find('.vertical-tab-button a').click(function () {
google.maps.event.trigger(map, 'resize');
geofield_gmap_center(params.mapid);
});
// Show all map tiles when a map is shown in a collapsible fieldset.
jQuery('#' + params.mapid).closest('fieldset.collapsible').find('a.fieldset-title').click(function () {
google.maps.event.trigger(map, 'resize');
geofield_gmap_center(params.mapid);
});
});
// Place map marker.
var marker = new google.maps.Marker({
map: map,
draggable: params.widget
});
geofield_gmap_data[params.mapid].marker = marker;
marker.setPosition(location);
if (params.widget && params.latid && params.lngid) {
geofield_gmap_data[params.mapid].lat = jQuery("#" + params.latid);
geofield_gmap_data[params.mapid].lng = jQuery("#" + params.lngid);
if (params.searchid) {
geofield_gmap_data[params.mapid].search = jQuery("#" + params.searchid);
geofield_gmap_data[params.mapid].search.autocomplete({
// This bit uses the geocoder to fetch address values.
source: function (request, response) {
geofield_gmap_geocoder.geocode({'address': request.term }, function (results, status) {
response(jQuery.map(results, function (item) {
return {
label: item.formatted_address,
value: item.formatted_address,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
};
}));
});
},
// This bit is executed upon selection of an address.
select: function (event, ui) {
geofield_gmap_data[params.mapid].lat.val(ui.item.latitude);
geofield_gmap_data[params.mapid].lng.val(ui.item.longitude);
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
marker.setPosition(location);
map.setCenter(location);
}
});
// Geocode user input on enter.
geofield_gmap_data[params.mapid].search.keydown(function (e) {
if (e.which == 13) {
var input = geofield_gmap_data[params.mapid].search.val();
// Execute the geocoder
geofield_gmap_geocoder.geocode({'address': input }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
// Set the location
var location = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
marker.setPosition(location);
map.setCenter(location);
// Fill the lat/lon fields with the new info
geofield_gmap_data[params.mapid].lat.val(marker.getPosition().lat());
geofield_gmap_data[params.mapid].lng.val(marker.getPosition().lng());
}
}
});
}
});
// Add listener to marker for reverse geocoding.
google.maps.event.addListener(marker, 'drag', function () {
geofield_gmap_geocoder.geocode({'latLng': marker.getPosition()}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
geofield_gmap_data[params.mapid].search.val(results[0].formatted_address);
geofield_gmap_data[params.mapid].lat.val(marker.getPosition().lat());
geofield_gmap_data[params.mapid].lng.val(marker.getPosition().lng());
}
}
});
});
}
if (params.click_to_place_marker) {
// Change marker position with mouse click.
google.maps.event.addListener(map, 'click', function (event) {
var position = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
marker.setPosition(position);
geofield_gmap_data[params.mapid].lat.val(position.lat());
geofield_gmap_data[params.mapid].lng.val(position.lng());
//google.maps.event.trigger(geofield_gmap_data[params.mapid].map, 'resize');
});
}
geofield_onchange = function () {
var location = new google.maps.LatLng(
parseInt(geofield_gmap_data[params.mapid].lat.val()),
parseInt(geofield_gmap_data[params.mapid].lng.val()));
marker.setPosition(location);
map.setCenter(location);
};
geofield_gmap_data[params.mapid].lat.change(geofield_onchange);
geofield_gmap_data[params.mapid].lng.change(geofield_onchange);
}
}