-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
96 lines (83 loc) · 3.2 KB
/
map.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
var directionsService;
var directionsDisplay;
function initMap() {
var uluru = {lat: 55.894424, lng: -3.685716};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: uluru
});
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, stations) {
directionsService = new google.maps.DirectionsService;
directionsDisplay = [];
directionsDisplay = new google.maps.DirectionsRenderer({
map: map,
suppressMarkers: true,
preserveViewport: true,
polylineOptions: {strokeColor: '#20395F'}
});
directionsService.route({
origin: {lat: parseFloat(stations[0]['latitude']), lng: parseFloat(stations[0]['longitude'])},
destination: {
lat: parseFloat(stations[stations.length - 1]['latitude']),
lng: parseFloat(stations[stations.length - 1]['longitude'])
},
travelMode: google.maps.TravelMode.TRANSIT,
transitOptions: {
modes: ['RAIL']
}
}, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
map.setZoom(15);
}
function calculate_route_new(directionsService, directionsDisplay, stations, train_position) {
directionsService = new google.maps.DirectionsService;
if (polyline !== undefined)
polyline.setMap(null);
var request = {
origin: {lat: parseFloat(stations[0]['latitude']), lng: parseFloat(stations[0]['longitude'])},
destination: {
lat: parseFloat(stations[stations.length - 1]['latitude']),
lng: parseFloat(stations[stations.length - 1]['longitude'])
},
travelMode: google.maps.TravelMode.TRANSIT,
transitOptions: {
modes: ['RAIL']
}
};
var polyline = new google.maps.Polyline({
path: [],
strokeColor: '#20395F',
strokeWeight: 3
});
directionsService.route(request, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
var bounds = new google.maps.LatLngBounds();
var route = response.routes[0];
var startLocation = {};
var endLocation = {};
var path = response.routes[0].overview_path;
var legs = response.routes[0].legs;
for (var i = 0; i < legs.length; i++) {
endLocation.latlng = legs[i].end_location;
endLocation.address = legs[i].end_address;
var steps = legs[i].steps;
for (j = 0; j < steps.length; j++) {
var nextSegment = steps[j].path;
for (var k = 0; k < nextSegment.length; k++) {
polyline.getPath().push(nextSegment[k]);
bounds.extend(nextSegment[k]);
}
}
}
polyline.setMap(map);
map.setZoom(10);
map.setCenter(train_position);
}
});
}