07_AnimationFeature.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_AnimationFeature"></title>
  9. <script type="text/javascript" src="../js/include-web.js"></script>
  10. <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
  11. </head>
  12. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  13. <div id="map" style="width: 100%;height:100%"></div>
  14. <script type="text/javascript">
  15. var url = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-china400/rest/maps/China_4326";
  16. new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
  17. var mapJSONObj = serviceResult.result;
  18. var map = new ol.Map({
  19. target: 'map',
  20. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  21. .extend([new ol.supermap.control.Logo()]),
  22. view: new ol.View({
  23. center: [103, 30],
  24. zoom: 4,
  25. projection: 'EPSG:4326',
  26. multiWorld: true
  27. })
  28. });
  29. var options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
  30. var layer = new ol.layer.Tile({
  31. source: new ol.source.TileSuperMapRest(options)
  32. });
  33. map.addLayer(layer);
  34. function addRandomFeature() {
  35. var xmax = 130, xmin = 80, ymax = 50, ymin = 20;
  36. var feature = new ol.Feature(new ol.geom.Point([Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)]));
  37. source.addFeature(feature);
  38. }
  39. function flash(feature) {
  40. var start = new Date().getTime();
  41. var listenerKey;
  42. function animate(event) {
  43. var duration = 3000;
  44. // ol6 废除了 event.vectorContext 接口,通过 ol.render.getVectorContext 来获取 vectorContext
  45. var vectorContext = new ol.render.getVectorContext(event);
  46. var frameState = event.frameState;
  47. var flashGeom = feature.getGeometry().clone();
  48. var elapsed = frameState.time - start;
  49. var elapsedRatio = elapsed / duration;
  50. var radius = ol.easing.easeOut(elapsedRatio) * 25 + 5;
  51. var opacity = ol.easing.easeOut(1 - elapsedRatio);
  52. var style = new ol.style.Style({
  53. image: new ol.style.Circle({
  54. radius: radius,
  55. stroke: new ol.style.Stroke({
  56. color: 'rgba(255, 0, 0, ' + opacity + ')',
  57. width: 0.25 + opacity
  58. })
  59. })
  60. });
  61. vectorContext.setStyle(style);
  62. vectorContext.drawGeometry(flashGeom);
  63. if (elapsed > duration) {
  64. ol.Observable.unByKey(listenerKey);
  65. return;
  66. }
  67. map.render();
  68. }
  69. listenerKey = vector.on('postrender', animate);
  70. }
  71. var source = new ol.source.Vector({
  72. wrapX: false
  73. });
  74. source.on('addfeature', function (e) {
  75. flash(e.feature);
  76. });
  77. var vector = new ol.layer.Vector({
  78. source: source
  79. });
  80. map.addLayer(vector);
  81. window.setInterval(addRandomFeature, 1000);
  82. });
  83. </script>
  84. </body>
  85. </html>