-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
188 lines (162 loc) · 6.74 KB
/
index.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html>
<head>
<title>TransitVis</title>
<link rel="stylesheet" href="openlayers/theme/default/style.css"
type="text/css">
<link rel="stylesheet" href="openlayers/examples/style.css"
type="text/css">
<script src="openlayers/lib/OpenLayers.js"></script>
<script src="js/jquery-1.7.2.js" type="text/javascript"></script>
<style type="text/css">
html, body, #map {
margin: 0;
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript">
var lon = 149.1305;
var lat = -35.2785;
var zoom = 14;
var map;
var size = new OpenLayers.Size(16,16);
calculateOffset = function(size) {
return new OpenLayers.Pixel(-(size.w/2), -size.h/2);
};
var icon = new OpenLayers.Icon(
"img/Anonymous_aiga_bus.png", size, null, calculateOffset
);
function update() {
if(
map.getLayersByName("Busses").length < 1 ||
map.getLayersByName("Shapes").length < 1
) {
return;
}
var busses = map.getLayersByName("Busses")[0];
var shapes = map.getLayersByName("Shapes")[0];
for(i=0;i<busses.markers.length;++i) {
var marker = busses.markers[i];
// this is the lazy way to advance the marker's position
// while I work out how to load and manage the trips
marker.data.distance += 0.01;
if( marker.data.shape ) {
var shape = marker.data.shape;
var shape_dist_traveled = shape.attributes.shape_dist_traveled;
var track = shape.geometry.components;
// this updates our 'cached' index of the last
// shape point we passed
while(
marker.data.distance >
shape_dist_traveled[ marker.data.index+1 ]
) {
marker.data.index++;
// hackey overflow protection
// this loops the marker around to the start
if( marker.data.index >= track.length -1 ) {
marker.data.index = 0;
marker.data.distance = 0;
}
}
// this is the normalised distance we have traveled
// after the point we just passed but before the next
// point on the shape.
var ratio = (
marker.data.distance -
shape_dist_traveled[marker.data.index]
) / (
shape_dist_traveled[marker.data.index+1] -
shape_dist_traveled[marker.data.index]
);
// finally update our marker position to the position
// of the point we just passed plus some of the distance
// between this and the next point based on how far along
// the shape segment we are.
marker.lonlat = new OpenLayers.LonLat(
track[marker.data.index].x + (
track[marker.data.index+1].x -
track[marker.data.index].x
)*ratio,
track[marker.data.index].y + (
track[marker.data.index+1].y -
track[marker.data.index].y
)*ratio
);
} else {
if(shape = shapes.getFeatureByFid(marker.data.shape_id) ) {
marker.data.shape = shape;
marker.icon.imageDiv.hidden=0;
}
}
}
busses.redraw();
t=setTimeout('update()',50);
}
function trips_received(data) {
var busses = map.getLayersByName("Busses")[0];
var shapes = map.getLayersByName("Shapes")[0];
for(trip_index in data) {
var marker =
new OpenLayers.Marker(
new OpenLayers.LonLat(0,0),
icon.clone()
);
marker.icon.imageDiv.hidden=1;
marker.data = {};
marker.data.index = 0;
marker.data.distance = parseFloat(data[trip_index].shape_dist_traveled);
marker.data.shape_id = data[trip_index].shape_id;
busses.addMarker(marker);
if(shape = shapes.getFeatureByFid(marker.data.shape_id) ) {
marker.data.shape = shape;
} else {
$.ajax({
url:"ajax.php",
method:'GET',
data:{'func':'shape-pts','id':data[trip_index].shape_id},
success:function(data){
var reader = new OpenLayers.Format.GeoJSON();
var feature = reader.read(data);
feature[0].geometry.transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
);
var shapes = map.getLayersByName("Shapes")[0];
if( shapes ) {
shapes.addFeatures(feature);
shapes.redraw();
}
}
});
}
}
}
function init(){
map = new OpenLayers.Map( 'map' );
map.addControl(new OpenLayers.Control.LayerSwitcher());
//gets us some background mapness
var osm = new OpenLayers.Layer.OSM( "Open Street Map");
map.addLayer(osm);
// Load the OpenStreetMap projection so we can translate
// Lon/Lat pairs to fit on the map
var proj = new OpenLayers.Projection("EPSG:4326");
var point = new OpenLayers.LonLat(lon,lat);
point.transform(proj,map.getProjectionObject());
map.setCenter(point, zoom);
map.addLayer(new OpenLayers.Layer.Vector("Shapes"));
map.addLayer(new OpenLayers.Layer.Markers("Busses"));
update();
$.ajax({
url:"ajax.php",
method:'GET',
data:{'func':'running'},
success:trips_received
});
}
</script>
</head>
<body onload="init()">
<div id="map"></div>
</body>
</html>