Repository navigation
Expand file tree
/
Copy pathbasic_bus_map.html
More file actions
141 lines (117 loc) · 3.82 KB
/
Copy pathbasic_bus_map.html
File metadata and controls
141 lines (117 loc) · 3.82 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Bus Test</title>
<meta http-equiv="refresh" content="45">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
#map {
width: 1260px;
height: 600px;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var bus_icon = L.icon({
iconUrl: 'metro_bus.png',
iconSize: [30, 30], // size of the icon
popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});
// Draw the basic map that we can add layers to
var map = L.map('map',{scrollWheelZoom:true}).setView([38.887546, -77.090105], 14);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Get all the bus positions, and add them as markers on the map
$(function() {
var params = {
"api_key": "",
};
$.ajax({
url: "https://api.wmata.com/Bus.svc/json/jBusPositions?" + $.param(params),
type: "GET",
})
.done(function(data) {
var bus_position_data = data["BusPositions"]
for (var i in bus_position_data)
{
var bus_id = bus_position_data[i].VehicleID;
var bus_route = bus_position_data[i].RouteID;
var direction = bus_position_data[i].DirectionText;
var bus_lat = bus_position_data[i].Lat;
var bus_lon = bus_position_data[i].Lon;
// Bind an onClick() function to each marker, so that the route for that bus will be shown when it's clicked
var marker = L.marker([bus_lat, bus_lon], {icon: bus_icon}).bindPopup(bus_id + ", " + bus_route).addTo(map).on('click', L.bind(onClick, null, bus_route, direction));
}
})
.fail(function() {
alert("error");
});
});
// Handle fetching the route when a bus marker is clicked
function onClick(route, direction) {
clearMap();
// alert(direction);
$(function() {
var params = {
"api_key": "",
// Request parameters
"RouteID": route,
};
$.ajax({
url: "https://api.wmata.com/Bus.svc/json/jRouteDetails?" + $.param(params),
type: "GET",
})
.done(function(data) {
// Buses don't run on the same streets if in both directions of a route.
// Figure out which direction this bus is going and draw the right route.
var route0_dir = data["Direction0"]["DirectionText"];
var route1_dir = data["Direction1"]["DirectionText"];
var route_data = ''
if (route0_dir == direction)
{
route_data = data["Direction0"]["Shape"];
}
else if (route1_dir == direction)
{
route_data = data["Direction1"]["Shape"];
}
var coord_array = {"coords": []};
for (var i in route_data)
{
var lat = route_data[i].Lat;
var lon = route_data[i].Lon;
coord_array['coords'].push([lat, lon]);
}
var polyline = L.polyline(coord_array['coords'], {
color: 'red',
weight: 5,
opacity: .9,
lineJoin: 'round'
}).addTo(map);
})
.fail(function() {
alert("error");
});
});
}
// Clear the map each time a bus is selected so that the new route can be drawn
function clearMap() {
for(i in map._layers) {
if(map._layers[i]._path != undefined) {
try {
map.removeLayer(map._layers[i]);
}
catch(e) {
console.log("problem with " + e + map._layers[i]);
}
}
}
}
</script>
</body>
</html>