-
Notifications
You must be signed in to change notification settings - Fork 821
/
index.ts
131 lines (116 loc) · 3.77 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
130
131
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [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.MapMouseEvent | 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.MapMouseEvent | 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();
if (event.placeId) {
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,
})
.then((response) => {
me.directionsRenderer.setDirections(response);
})
.catch((e) => window.alert("Directions request failed due to " + status));
}
getPlaceInformation(placeId: string) {
const me = this;
this.placesService.getDetails(
{ placeId: placeId },
(
place: google.maps.places.PlaceResult | null,
status: google.maps.places.PlacesServiceStatus
) => {
if (
status === "OK" &&
place &&
place.geometry &&
place.geometry.location
) {
me.infowindow.close();
me.infowindow.setPosition(place.geometry.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);
}
}
);
}
}
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
// [END maps_event_poi]
export {};