-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstops.js
161 lines (138 loc) · 5 KB
/
stops.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var map;
var mapProp = {
center: new google.maps.LatLng(42.3600, -71.0589),
zoom: 12,
};
var myLat = 0;
var myLng = 0;
var dotIcon = "dot.png";
var infowindow = new google.maps.InfoWindow();
function myMap() {
map = new google.maps.Map(document.getElementById("map"), mapProp);
loadUserLocation();
loadTrains();
}
function loadUserLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLat = position.coords.latitude;
myLng = position.coords.longitude;
addUserToMap();
})
} else {
document.getElementById("location").innerHTML = "Geolocation is not supported by this browser.";
}
}
function addUserToMap() {
//me = new google.maps.LatLng(myLat, myLng);
//map.panTo(me); // pans map to user location
addToMap(myLat, myLng, "User");
}
function addToMap(lat, lng, name) {
if (name == "User"){
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
title: name,
icon: dotIcon
});
} else {
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
title: name,
});
}
marker.setMap(map);
google.maps.event.addListener(marker, 'click', function() {
//infowindow.setContent(this.title);
//infowindow.open(map, this);
if(name != 'User'){
getStopInfo(this.title);
}
});
}
function loadTrains(){
request = new XMLHttpRequest();
request.open("GET", "http://realtime.mbta.com/developer/api/v2/stopsbyroute?api_key=wX9NwuHnZU2ToO7GmGR9uw&route=Red&format=json", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200){
var list = JSON.parse(request.responseText);
var stop;
for (var i = 0; i < list.direction.length; i++){
for (var j = 0; j < list.direction[i].stop.length; j++){
stop = list.direction[i].stop[j];
var label = stop.parent_station;
addToMap(stop.stop_lat, stop.stop_lon, label);
}
}
}
};
request.send();
}
function getStopInfo(stop_string){
var stoprequest = new XMLHttpRequest();
var req_string = ('http://realtime.mbta.com/developer/api/v2/predictionsbystop?api_key=wX9NwuHnZU2ToO7GmGR9uw&stop='
+ stop_string + '&format=json');
stoprequest.open("GET", req_string, true);
stoprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
stoprequest.onreadystatechange = displayStopInfo;
stoprequest.send();
}
function displayStopInfo(){
if (this.readyState == 4 && this.status == 200){
var responseObject = JSON.parse(this.responseText);
for (i in responseObject.mode){
if (responseObject.mode[i].mode_name == "Subway"){
for (j in responseObject.mode[i].route){
if (responseObject.mode[i].route[j].route_id == 'Red'){
postRouteText(responseObject.mode[i].route[j]);
}
}
}
}
postAlerts(responseObject, responseObject.stop_name);
} else {
document.getElementById("stop-info-south").innerHTML =
document.getElementById("stop-info-north").innerHTML = "<p>No Trains Available</p>";
document.getElementById("stop-name").innerHTML = "Station";
}
}
function postAlerts(responseObject, stop_name){
document.getElementById("stop-name").innerHTML = stop_name;
if (responseObject.alert_headers.length != 0){
document.getElementById("alerts").innerHTML = '<p class="center">ALERTS</p>';
}
for (i in responseObject.alert_headers) {
document.getElementById("alerts").innerHTML += '<p>' +
responseObject.alert_headers[i].header_text + '</p>';
}
}
function postRouteText(list, stop_name){
for (var i = 0; i < list.direction.length; i ++){
var text = '<p>' + list.direction[i].direction_name + '</p> <ul>';
for (var j = 0; j < list.direction[i].trip.length; j++){
var trip = list.direction[i].trip[j];
text += '<li>' + ('0' + getDateTime(trip).getHours()).slice(-2) + ':'
+ ('0' + (getDateTime(trip).getMinutes())).slice(-2) + '</li>';
}
text += '</ul>'
if (list.direction[i].direction_id == 0){
document.getElementById("stop-info-south").innerHTML = text;
} else {
document.getElementById("stop-info-north").innerHTML = text;
}
}
}
function getDateTime(trip){
var dt;
if (typeof trip.sch_dep_dt != 'undefined'){
dt = trip.sch_dep_dt;
} else if (typeof trip.pre_dt != 'undefined'){
dt = trip.pre_dt;
} else {
dt = 0;
}
var time = new Date(0);
time.setUTCSeconds(dt);
return time;
}