/
index.ts
129 lines (119 loc) · 4.14 KB
/
index.ts
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
/*
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START maps_event_poi]
function initMap(): void {
const origin = { lat: -33.871, lng: 151.197 };
const map = new google.maps.Map(
document.getElementById("map") as HTMLElement,
{
zoom: 18,
center: origin,
}
);
new ClickEventHandler(map, origin);
}
function isIconMouseEvent(
e: google.maps.MouseEvent | google.maps.IconMouseEvent
): e is google.maps.IconMouseEvent {
return "placeId" in e;
}
class ClickEventHandler {
origin: google.maps.LatLngLiteral;
map: google.maps.Map;
directionsService: google.maps.DirectionsService;
directionsRenderer: google.maps.DirectionsRenderer;
placesService: google.maps.places.PlacesService;
infowindow: google.maps.InfoWindow;
infowindowContent: HTMLElement;
constructor(map: google.maps.Map, origin: google.maps.LatLngLiteral) {
this.origin = origin;
this.map = map;
this.directionsService = new google.maps.DirectionsService();
this.directionsRenderer = new google.maps.DirectionsRenderer();
this.directionsRenderer.setMap(map);
this.placesService = new google.maps.places.PlacesService(map);
this.infowindow = new google.maps.InfoWindow();
this.infowindowContent = document.getElementById(
"infowindow-content"
) as HTMLElement;
this.infowindow.setContent(this.infowindowContent);
// Listen for clicks on the map.
this.map.addListener("click", this.handleClick.bind(this));
}
handleClick(event: google.maps.MouseEvent | google.maps.IconMouseEvent) {
console.log("You clicked on: " + event.latLng);
// If the event has a placeId, use it.
if (isIconMouseEvent(event)) {
console.log("You clicked on place:" + event.placeId);
// Calling e.stop() on the event prevents the default info window from
// showing.
// If you call stop here when there is no placeId you will prevent some
// other map click event handlers from receiving the event.
event.stop();
this.calculateAndDisplayRoute(event.placeId);
this.getPlaceInformation(event.placeId);
}
}
calculateAndDisplayRoute(placeId: string) {
const me = this;
this.directionsService.route(
{
origin: this.origin,
destination: { placeId: placeId },
travelMode: google.maps.TravelMode.WALKING,
},
(response, status) => {
if (status === "OK") {
me.directionsRenderer.setDirections(response);
} else {
window.alert("Directions request failed due to " + status);
}
}
);
}
getPlaceInformation(placeId: string) {
const me = this;
this.placesService.getDetails(
{ placeId: placeId },
(
place: google.maps.places.PlaceResult,
status: google.maps.places.PlacesServiceStatus
) => {
if (status === "OK") {
me.infowindow.close();
me.infowindow.setPosition(
(place.geometry as google.maps.places.PlaceGeometry).location
);
(me.infowindowContent.children[
"place-icon"
] as HTMLImageElement).src = place.icon as string;
(me.infowindowContent.children[
"place-name"
] as HTMLElement).textContent = place.name;
(me.infowindowContent.children[
"place-id"
] as HTMLElement).textContent = place.place_id as string;
(me.infowindowContent.children[
"place-address"
] as HTMLElement).textContent = place.formatted_address as string;
me.infowindow.open(me.map);
}
}
);
}
}
// [END maps_event_poi]
export { initMap };