-
Notifications
You must be signed in to change notification settings - Fork 821
/
index.ts
121 lines (111 loc) · 3.78 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
/*
* 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.
*/
// @ts-nocheck TODO(jpoehnelt) remove when fixed
// [START maps_distance_matrix]
function initMap(): void {
const bounds = new google.maps.LatLngBounds();
const markersArray: google.maps.Marker[] = [];
const origin1 = { lat: 55.93, lng: -3.118 };
const origin2 = "Greenwich, England";
const destinationA = "Stockholm, Sweden";
const destinationB = { lat: 50.087, lng: 14.421 };
const destinationIcon =
"https://chart.googleapis.com/chart?" +
"chst=d_map_pin_letter&chld=D|FF0000|000000";
const originIcon =
"https://chart.googleapis.com/chart?" +
"chst=d_map_pin_letter&chld=O|FFFF00|000000";
const map = new google.maps.Map(
document.getElementById("map") as HTMLElement,
{
center: { lat: 55.53, lng: 9.4 },
zoom: 10,
}
);
const geocoder = new google.maps.Geocoder();
const service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false,
},
(response, status) => {
if (status !== "OK") {
alert("Error was: " + status);
} else {
const originList = response.originAddresses;
const destinationList = response.destinationAddresses;
const outputDiv = document.getElementById("output") as HTMLDivElement;
outputDiv.innerHTML = "";
deleteMarkers(markersArray);
const showGeocodedAddressOnMap = function (asDestination: boolean) {
const icon = asDestination ? destinationIcon : originIcon;
return function (
results: google.maps.GeocoderResult[],
status: google.maps.GeocoderStatus
) {
if (status === "OK") {
map.fitBounds(bounds.extend(results[0].geometry.location));
markersArray.push(
new google.maps.Marker({
map,
position: results[0].geometry.location,
icon: icon,
})
);
} else {
alert("Geocode was not successful due to: " + status);
}
};
};
for (let i = 0; i < originList.length; i++) {
const results = response.rows[i].elements;
geocoder.geocode(
{ address: originList[i] },
showGeocodedAddressOnMap(false)
);
for (let j = 0; j < results.length; j++) {
geocoder.geocode(
{ address: destinationList[j] },
showGeocodedAddressOnMap(true)
);
outputDiv.innerHTML +=
originList[i] +
" to " +
destinationList[j] +
": " +
results[j].distance.text +
" in " +
results[j].duration.text +
"<br>";
}
}
}
}
);
}
function deleteMarkers(markersArray: google.maps.Marker[]) {
for (let i = 0; i < markersArray.length; i++) {
markersArray[i].setMap(null);
}
markersArray = [];
}
// [END maps_distance_matrix]
export { initMap };