RideLogicAVLS.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 defaultLonLat = [ -76.5019, 42.4440];
  15. var defaultWebMerc = fromLonLat(defaultLonLat);
  16. function _rnd(a,b) {
  17. a = ((typeof a == "undefined") ? 1.0 : a);
  18. if (typeof b === "undefined") {
  19. return Math.random()*a;
  20. }
  21. return Math.random()*(b-a) + a;
  22. }
  23. //var vectorSource = new VectorSource({ features: [iconFeature], });
  24. var vectorSource = new VectorSource();
  25. var _icon = [];
  26. function example_change_pos() {
  27. var w = 1/16.0;
  28. let lonlat = [
  29. defaultLonLat[0] + _rnd(-w,w),
  30. defaultLonLat[1] + _rnd(-w,w)];
  31. let merc = fromLonLat(lonlat);
  32. _icon[0].feature.getGeometry().setCoordinates(merc);
  33. vectorLayer.getSource().changed();
  34. }
  35. function example_change_opacity(p) {
  36. p = ((typeof p === "undefined") ? 0.0 : p);
  37. _icon[0].style.getImage().setOpacity(p);
  38. vectorLayer.getSource().changed();
  39. }
  40. // icon can't change image src dynamically,
  41. // so have to replace.
  42. // See https://stackoverflow.com/questions/57341190/how-can-i-change-icon-source-dynamically
  43. //
  44. function example_change_image() {
  45. var _nuimg = new Icon({
  46. anchor: [0.5, 46],
  47. anchorXUnits: 'fraction',
  48. anchorYUnits: 'pixels',
  49. src: 'data/bus_gw_90.png',
  50. });
  51. _icon[0].style.setImage(_nuimg);
  52. vectorLayer.getSource().changed();
  53. }
  54. for (var ii=0; ii<10; ii++) {
  55. var w = 1/32.0;
  56. let lonlat = [
  57. defaultLonLat[0] + _rnd(-w,w),
  58. defaultLonLat[1] + _rnd(-w,w)];
  59. let merc = fromLonLat(lonlat);
  60. var iconFeature = new Feature({
  61. //geometry: new Point([10, 20]),
  62. geometry: new Point([merc[0], merc[1]]),
  63. name: 'Null Island',
  64. population: 4000,
  65. rainfall: 500,
  66. });
  67. var iconStyle = new Style({
  68. image: new Icon({
  69. anchor: [0.5, 46],
  70. anchorXUnits: 'fraction',
  71. anchorYUnits: 'pixels',
  72. src: 'data/icon.png',
  73. }),
  74. });
  75. _icon.push({ "style": iconStyle, "feature": iconFeature });
  76. iconFeature.setStyle(iconStyle);
  77. vectorSource.addFeature(iconFeature);
  78. }
  79. const vectorLayer = new VectorLayer({
  80. source: vectorSource,
  81. });
  82. const rasterLayer = new TileLayer({
  83. //source: ol.source.OSM()
  84. source: new ol.source.OSM(),
  85. });
  86. const map = new Map({
  87. layers: [rasterLayer, vectorLayer],
  88. target: document.getElementById('map'),
  89. view: new View({
  90. center: defaultWebMerc,
  91. zoom: 13,
  92. }),
  93. });
  94. const element = document.getElementById('popup');
  95. const popup = new Overlay({
  96. element: element,
  97. positioning: 'bottom-center',
  98. stopEvent: false,
  99. });
  100. map.addOverlay(popup);
  101. // display popup on click
  102. map.on('click', function (evt) {
  103. const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
  104. return feature;
  105. });
  106. });
  107. // change mouse cursor when over marker
  108. map.on('pointermove', function (e) {
  109. const pixel = map.getEventPixel(e.originalEvent);
  110. const hit = map.hasFeatureAtPixel(pixel);
  111. map.getTarget().style.cursor = hit ? 'pointer' : '';
  112. });
  113. // Close the popup when the map is moved
  114. map.on('movestart', function () {
  115. //$(element).popover('dispose');
  116. });
  117. map.on("postrender", function(event) {
  118. //console.log("bang...");
  119. });
  120. //----
  121. //
  122. setInterval( function() {
  123. //console.log("bang");
  124. //map.render();
  125. }, 1000);