-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocomplete-search.js
259 lines (226 loc) · 8.74 KB
/
autocomplete-search.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*global document, window, alert, console, require, google*/
var AddLocation, CalculateButton, calculateAndDisplayRoute, updateButtons, verticesPanel;
/*
This adds a search box to a map, using the Google Place Autocomplete
feature. People can enter their places of interest. The search box will return a
suggestions dropbar containing a mix of places and predicted search terms.
Used in: CalculateButton, calculateAndDisplayRoute, createList, verticesPanel
*/
function initAutocomplete() {
"use strict";
var map = new google.maps.Map(document.getElementById('map'), {
backgroundColor: "#0099dd",
center: {
lat: 1.352083,
lng: 103.819839
}, //Singapore-centered
zoom: 3,
maxZoom: 15,
minZoom: 3,
mapTypeId: 'roadmap',
disableDefaultUI: true,
gestureHandling: 'greedy',
mapTypeControlOptions: {
mapTypeIds: []
},
styles: [
{
"stylers": [
{
"saturation": -100
}
]
},
{
"featureType": "water",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#0099dd"
}
]
},
{
"elementType": "labels",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#aadd55"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{}
]
}),
directionsService = new google.maps.DirectionsService(),
directionsDisplay = new google.maps.DirectionsRenderer(),
//Create the search box and link it to the UI element.
input = document.getElementById('pac-input'),
searchBox = new google.maps.places.SearchBox(input),
//Other variables: markers, temp - wrappedLocObj, locations, etc
result = {},
markers = [],
temp = [null],
locations = [],
addLocationDiv = document.createElement('div'),
addLocation = new AddLocation(addLocationDiv, map, temp, locations),
//Variables: strictBounds, lastValidCenter
strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(85, -180), // top left corner of map
new google.maps.LatLng(-85, 180) // bottom right corner
),
lastValidCenter = map.getCenter();
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('left-panel'));
input.addEventListener('focus', function () {
document.body.classList.add("keyboard");
});
input.addEventListener("blur", function () {
document.body.classList.remove("keyboard");
});
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(input);
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(addLocationDiv);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces(),
bounds = new google.maps.LatLngBounds();
if (places.length === 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
// Whenever we perform a search, if the search returns a unique place, add that place to a wrapped temp object.
// This wrapped temp object will be passed to the AddLocation function button if the button is clicked.
if (places.length === 1) {
temp[0] = places[0];
}
});
// Listen for the dragend event
google.maps.event.addListener(map, 'center_changed', function () {
if (strictBounds.contains(map.getCenter())) {
lastValidCenter = map.getCenter();
}
var c = map.getCenter(),
y = c.lat(),
//maxX = strictBounds.getNorthEast().lng(),
minY = strictBounds.getNorthEast().lat(),
//minX = strictBounds.getSouthWest().lng(),
maxY = strictBounds.getSouthWest().lat();
// not valid anymore => return to last valid position
if (y < minY || y > maxY) {
map.panTo(lastValidCenter);
} else {
lastValidCenter = map.getCenter();
}
});
// Nested functions in initAutocomplete below
function callCalculateAndDisplay(listOfLocations, mode) {
return calculateAndDisplayRoute(directionsService, directionsDisplay, listOfLocations, mode);
}
// Update button function called when loading a saved path
function callUpdateButtons(listOfLocations) {
// Remove current 'Add' controls at bottom center of map
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].removeAt(1);
// Create new addLocationDiv to erase existing location buttons behind the scenes
var newaddLocationDiv = document.createElement('div');
temp = [null];
addLocation = new AddLocation(newaddLocationDiv, map, temp, listOfLocations);
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(newaddLocationDiv);
// Update buttons for the locations in the saved paths only
updateButtons(listOfLocations, map);
}
function callVerticesPanel(listOfLocations) {
verticesPanel(map, result, listOfLocations);
}
function initCalculateButton(calculateButtonDiv, map, listOfLocations) {
return new CalculateButton(calculateButtonDiv, map, listOfLocations);
}
function setResult(response) {
result = response;
console.log(result);
}
function clearMap() {
directionsDisplay.setMap(null);
}
function resetMap() {
directionsDisplay.setMap(map);
}
initAutocomplete.callCalculateAndDisplay = callCalculateAndDisplay;
initAutocomplete.callUpdateButtons = callUpdateButtons;
initAutocomplete.callVerticesPanel = callVerticesPanel;
initAutocomplete.initCalculateButton = initCalculateButton;
initAutocomplete.setResult = setResult;
initAutocomplete.clearMap = clearMap;
initAutocomplete.resetMap = resetMap;
}