RideLogicAVLS.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. var Feature = ol.Feature;
  2. var Map = ol.Map;
  3. var Overlay = ol.Overlay;
  4. var Point = ol.geom.Point;
  5. var TileJSON = ol.source.TileJSON;
  6. var VectorSource = ol.source.Vector;
  7. var View = ol.View;
  8. var Icon = ol.style.Icon;
  9. var Style = ol.style.Style;
  10. var TileLayer = ol.layer.Tile;
  11. var VectorLayer = ol.layer.Vector;
  12. var OSM = ol.source.OSM;
  13. var fromLonLat = ol.proj.fromLonLat;
  14. var LineString = ol.geom.LineString;
  15. //var defaultLonLat = [ -76.5019, 42.4440];
  16. //var defaultLonLat = [ -76.1805, 42.6012 ];
  17. var defaultLonLat = [ -76.2705, 42.5512 ];
  18. var defaultWebMerc = fromLonLat(defaultLonLat);
  19. function _rnd(a,b) {
  20. a = ((typeof a == "undefined") ? 1.0 : a);
  21. if (typeof b === "undefined") {
  22. return Math.random()*a;
  23. }
  24. return Math.random()*(b-a) + a;
  25. }
  26. //var vectorSource = new VectorSource({ features: [iconFeature], });
  27. var vectorSource = new VectorSource();
  28. var _icon = [];
  29. function example_change_pos() {
  30. var w = 1/16.0;
  31. let lonlat = [
  32. defaultLonLat[0] + _rnd(-w,w),
  33. defaultLonLat[1] + _rnd(-w,w)];
  34. let merc = fromLonLat(lonlat);
  35. _icon[0].feature.getGeometry().setCoordinates(merc);
  36. busLayer.getSource().changed();
  37. }
  38. function example_change_opacity(p) {
  39. p = ((typeof p === "undefined") ? 0.0 : p);
  40. _icon[0].style.getImage().setOpacity(p);
  41. busLayer.getSource().changed();
  42. }
  43. // icon can't change image src dynamically,
  44. // so have to replace.
  45. // See https://stackoverflow.com/questions/57341190/how-can-i-change-icon-source-dynamically
  46. //
  47. function example_change_image() {
  48. var _nuimg = new Icon({
  49. anchor: [0.5, 46],
  50. anchorXUnits: 'fraction',
  51. anchorYUnits: 'pixels',
  52. src: 'data/bus_gw_90.png',
  53. });
  54. _icon[0].style.setImage(_nuimg);
  55. busLayer.getSource().changed();
  56. }
  57. for (var ii=0; ii<2; ii++) {
  58. var w = 1/32.0;
  59. let lonlat = [
  60. defaultLonLat[0] + _rnd(-w,w),
  61. defaultLonLat[1] + _rnd(-w,w)];
  62. let merc = fromLonLat(lonlat);
  63. var iconFeature = new Feature({
  64. //geometry: new Point([10, 20]),
  65. geometry: new Point([merc[0], merc[1]]),
  66. name: 'Null Island',
  67. population: 4000,
  68. rainfall: 500,
  69. });
  70. var iconStyle = new Style({
  71. image: new Icon({
  72. anchor: [0.5, 46],
  73. anchorXUnits: 'fraction',
  74. anchorYUnits: 'pixels',
  75. src: 'data/icon.png',
  76. }),
  77. });
  78. iconStyle.getImage().setOpacity(0.0);
  79. _icon.push({ "style": iconStyle, "feature": iconFeature });
  80. iconFeature.setStyle(iconStyle);
  81. vectorSource.addFeature(iconFeature);
  82. }
  83. const busLayer = new VectorLayer({
  84. source: vectorSource,
  85. zIndex: 1
  86. });
  87. //--
  88. const rasterLayer = new TileLayer({
  89. //source: ol.source.OSM()
  90. source: new ol.source.OSM(),
  91. });
  92. //--
  93. const map = new Map({
  94. //layers: [rasterLayer, vectorLayer, routeLayer],
  95. layers: [rasterLayer, busLayer ],
  96. target: document.getElementById('map'),
  97. view: new View({
  98. center: defaultWebMerc,
  99. zoom: 11,
  100. }),
  101. });
  102. const element = document.getElementById('popup');
  103. const popup = new Overlay({
  104. element: element,
  105. positioning: 'bottom-center',
  106. stopEvent: false,
  107. });
  108. map.addOverlay(popup);
  109. // display popup on click
  110. map.on('click', function (evt) {
  111. const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
  112. return feature;
  113. });
  114. });
  115. // change mouse cursor when over marker
  116. map.on('pointermove', function (e) {
  117. const pixel = map.getEventPixel(e.originalEvent);
  118. const hit = map.hasFeatureAtPixel(pixel);
  119. map.getTarget().style.cursor = hit ? 'pointer' : '';
  120. });
  121. // Close the popup when the map is moved
  122. map.on('movestart', function () {
  123. //$(element).popover('dispose');
  124. });
  125. map.on("postrender", function(event) {
  126. //console.log("bang...");
  127. });
  128. //----
  129. //
  130. setInterval( function() {
  131. //console.log("bang");
  132. //map.render();
  133. }, 1000);
  134. //-----
  135. //
  136. function update_buses(json_gtfs) {
  137. var bearingIdxLookup = [ "0", "45", "90", "135", "180", "225", "270", "315" ];
  138. for (let ii=0; ii<json_gtfs.entityList.length; ii++) {
  139. let id = json_gtfs.entityList[ii].id;
  140. let p = json_gtfs.entityList[ii].vehicle.position;
  141. let lat = p.latitude;
  142. let lon = p.longitude;
  143. let bearing = p.bearing;
  144. let lonlat = [ lon , lat ];
  145. let merc = fromLonLat(lonlat);
  146. _icon[ii].feature.getGeometry().setCoordinates(merc);
  147. bearing = (bearing % 360 );
  148. if (bearing < 0) { bearing += 360.0; }
  149. let iheading = Math.floor( (parseInt( bearing ) + 23) / 45 );
  150. if (iheading > 7) { iheading = 0; }
  151. var _nuimg = new Icon({
  152. anchor: [0.5, 46],
  153. anchorXUnits: 'fraction',
  154. anchorYUnits: 'pixels',
  155. src: 'data/bus_gw_' + bearingIdxLookup[iheading] + '.png',
  156. });
  157. _icon[ii].style.setImage(_nuimg);
  158. _icon[ii].style.getImage().setOpacity(1.0);
  159. console.log(">>", id, lat, lon, bearing);
  160. }
  161. busLayer.getSource().changed();
  162. }
  163. function fetch_gtfs_vehicle_position() {
  164. let xhr = new XMLHttpRequest();
  165. xhr.onload = function() {
  166. var ab = xhr.response;
  167. var json_pb = pb2json.pb2json(ab);
  168. console.log(">>>", json_pb);
  169. update_buses(json_pb)
  170. };
  171. xhr.responseType = "arraybuffer";
  172. xhr.open("GET", "VehiclePosition.pb");
  173. xhr.send();
  174. }
  175. function update_route_layer(geojson) {
  176. var route_points = geojson.features[0].geometry.coordinates;
  177. var merc_points = [];
  178. for (let ii=0; ii<route_points.length; ii++) {
  179. merc_points.push(ol.proj.transform(route_points[ii], 'EPSG:4326', 'EPSG:3857'));
  180. }
  181. var routeLine = new ol.Feature({
  182. geometry: new ol.geom.LineString(merc_points)
  183. });
  184. var routeLineVector = new ol.source.Vector({});
  185. routeLineVector.addFeature(routeLine);
  186. var routeLayer = new VectorLayer({
  187. source: routeLineVector,
  188. style: new ol.style.Style({
  189. fill: new ol.style.Fill({ color: 'rgb(0,0,255,0.25)', weight: 4, opacity: 0.5 }),
  190. stroke: new ol.style.Stroke({ color: 'rgb(0,0,255,0.25)', width: 6, opacity: 0.5 })
  191. }),
  192. zIndex: 0
  193. });
  194. map.addLayer(routeLayer);
  195. }
  196. function fetch_geojson_route(url) {
  197. let xhr = new XMLHttpRequest();
  198. xhr.onload = function() {
  199. var geojson = xhr.response;
  200. update_route_layer(geojson);
  201. };
  202. xhr.responseType = "json";
  203. xhr.open("GET", url, true);
  204. xhr.send();
  205. }
  206. function init() {
  207. setInterval(fetch_gtfs_vehicle_position, 1000);
  208. setTimeout( function() { fetch_geojson_route("geojson/test_route.geojson"); }, 100);
  209. }
  210. init();