-
Notifications
You must be signed in to change notification settings - Fork 822
/
Copy pathindex.js
125 lines (114 loc) · 3.69 KB
/
index.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
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_places_autocomplete_directions]
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script
// src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
mapTypeControl: false,
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
});
new AutocompleteDirectionsHandler(map);
}
class AutocompleteDirectionsHandler {
map;
originPlaceId;
destinationPlaceId;
travelMode;
directionsService;
directionsRenderer;
constructor(map) {
this.map = map;
this.originPlaceId = "";
this.destinationPlaceId = "";
this.travelMode = google.maps.TravelMode.WALKING;
this.directionsService = new google.maps.DirectionsService();
this.directionsRenderer = new google.maps.DirectionsRenderer();
this.directionsRenderer.setMap(map);
const originInput = document.getElementById("origin-input");
const destinationInput = document.getElementById("destination-input");
const modeSelector = document.getElementById("mode-selector");
// Specify just the place data fields that you need.
const originAutocomplete = new google.maps.places.Autocomplete(
originInput,
{ fields: ["place_id"] },
);
// Specify just the place data fields that you need.
const destinationAutocomplete = new google.maps.places.Autocomplete(
destinationInput,
{ fields: ["place_id"] },
);
this.setupClickListener(
"changemode-walking",
google.maps.TravelMode.WALKING,
);
this.setupClickListener(
"changemode-transit",
google.maps.TravelMode.TRANSIT,
);
this.setupClickListener(
"changemode-driving",
google.maps.TravelMode.DRIVING,
);
this.setupPlaceChangedListener(originAutocomplete, "ORIG");
this.setupPlaceChangedListener(destinationAutocomplete, "DEST");
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(
destinationInput,
);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector);
}
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
setupClickListener(id, mode) {
const radioButton = document.getElementById(id);
radioButton.addEventListener("click", () => {
this.travelMode = mode;
this.route();
});
}
setupPlaceChangedListener(autocomplete, mode) {
autocomplete.bindTo("bounds", this.map);
autocomplete.addListener("place_changed", () => {
const place = autocomplete.getPlace();
if (!place.place_id) {
window.alert("Please select an option from the dropdown list.");
return;
}
if (mode === "ORIG") {
this.originPlaceId = place.place_id;
} else {
this.destinationPlaceId = place.place_id;
}
this.route();
});
}
route() {
if (!this.originPlaceId || !this.destinationPlaceId) {
return;
}
const me = this;
this.directionsService.route(
{
origin: { placeId: this.originPlaceId },
destination: { placeId: this.destinationPlaceId },
travelMode: this.travelMode,
},
(response, status) => {
if (status === "OK") {
me.directionsRenderer.setDirections(response);
} else {
window.alert("Directions request failed due to " + status);
}
},
);
}
}
window.initMap = initMap;
// [END maps_places_autocomplete_directions]