-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras.js
65 lines (58 loc) · 2.3 KB
/
extras.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
function showFeatureDistanceFromUserLocation() {
navigator.geolocation.getCurrentPosition(function(position) {
userLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var distanceToArray = new Array();
var distanceToLocation = new Array();
map.data.forEach(function(feature) {
distanceToArray.push(feature.getGeometry().get());
});
showDrivingDistancesFromLatLng(userLocation, distanceToArray);
});
};
function showDrivingDistancesFromLatLng(origin, positions) {
var service = new google.maps.DistanceMatrixService();
drivingDistancePanel.innerHTML = "";
do {
var positionSplice = positions.splice(0, positions.length > 25 ? 25 : positions.length);
service.getDistanceMatrix({
origins: [origin],
destinations: positionSplice,
travelMode: 'DRIVING',
}, callback);
} while (positions.length > 0);
function callback(response, status) {
var row;
var originIndex = 0;
for (row of response.rows) {
var element;
var destinationIndex = 0;
if (isDivInnerHTMLEmpty(drivingDistancePanel)) {
addStringToDivInnerHTML(drivingDistancePanel, "Driving distance from " + response.originAddresses[originIndex] + ":<br/>");
}
for (element of row.elements) {
var duration, distance;
try {
distance = element.distance.text;
} catch (e) {
distance = 'unknown';
}
try {
duration = element.duration.text;
} catch (e) {
duration = 'unknown';
}
addStringToDivInnerHTML(drivingDistancePanel, "To " + response.destinationAddresses[destinationIndex] + ", " + distance + " can drive in " + duration + "<br/>");
destinationIndex++;
}
originIndex++; //support multiple for future implementation
}
}
}
function exportJSON() {
map.data.toGeoJson(function(obj) {
document.getElementById('geoJson').innerHTML = JSON.stringify(obj, null, 2);
});
}
function clearJSON() {
document.getElementById('geoJson').innerHTML = "";
}