dataFlowService.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_dataFlowService"></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. <style>
  12. .ol-popup {
  13. position: absolute;
  14. background-color: white;
  15. -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  16. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  17. padding: 15px;
  18. border-radius: 10px;
  19. border: 1px solid #cccccc;
  20. bottom: 12px;
  21. left: -50px;
  22. min-width: 210px;
  23. }
  24. .ol-popup:after,
  25. .ol-popup:before {
  26. top: 100%;
  27. border: solid transparent;
  28. content: " ";
  29. height: 0;
  30. width: 0;
  31. position: absolute;
  32. pointer-events: none;
  33. }
  34. .ol-popup:after {
  35. border-top-color: white;
  36. border-width: 10px;
  37. left: 48px;
  38. margin-left: -10px;
  39. }
  40. .ol-popup:before {
  41. border-top-color: #cccccc;
  42. border-width: 11px;
  43. left: 48px;
  44. margin-left: -11px;
  45. }
  46. </style>
  47. </head>
  48. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  49. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  50. <div id="popup" class="ol-popup">
  51. <div id="popup-content"></div>
  52. </div>
  53. <script type="text/javascript">
  54. var container = document.getElementById("popup");
  55. var content = document.getElementById("popup-content");
  56. var overlay = new ol.Overlay({
  57. element: container,
  58. autoPan: true,
  59. autoPanAnimation: {
  60. duration: 250
  61. }
  62. });
  63. var resultLayer,
  64. urlQuery = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china400/rest/maps/China_4326",
  65. wsHost = "wss:\//" + (window.isLocal ? document.location.hostname + ":8800" : "iclsvrws.supermap.io");
  66. var urlDataFlow = wsHost + "/iserver/services/dataflowTest/dataflow";
  67. var timer, featureResult, dataFlowBroadcast, source;
  68. new ol.supermap.MapService(urlQuery).getMapInfo(function(serviceResult) {
  69. var mapJSONObj = serviceResult.result;
  70. //初始化地图
  71. var map = new ol.Map({
  72. target: "map",
  73. controls: ol.control
  74. .defaults({
  75. attributionOptions: {
  76. collapsed: false
  77. }
  78. })
  79. .extend([new ol.supermap.control.Logo()]),
  80. view: new ol.View({
  81. center: [116.443571, 39.887549],
  82. zoom: 12,
  83. projection: "EPSG:4326",
  84. multiWorld: true
  85. }),
  86. overlays: [overlay]
  87. });
  88. //添加底图
  89. var layer = new ol.layer.Tile({
  90. source: new ol.source.TileSuperMapRest(ol.source.TileSuperMapRest.optionsFromMapJSON(urlQuery, mapJSONObj))
  91. });
  92. map.addLayer(layer);
  93. SuperMap.SecurityManager.registerToken(urlDataFlow, window.exampleToken);
  94. //添加dataflow
  95. var source = new ol.source.DataFlow({
  96. ws: urlDataFlow
  97. });
  98. source.on("dataupdated", function(e) {
  99. var feature = e.value.data;
  100. content.innerHTML = feature.get("time");
  101. overlay.setPosition(feature.getGeometry().getCoordinates());
  102. });
  103. resultLayer = new ol.layer.Vector({
  104. source: source,
  105. style: new ol.style.Style({
  106. image: new ol.style.Circle({
  107. fill: new ol.style.Fill({
  108. color: "rgba(255,0,0,0.9)"
  109. }),
  110. radius: 6
  111. })
  112. })
  113. });
  114. //模拟实时数据 start
  115. //查询一个线数据,每两秒将一个点通过dataFlowService广播给iSevrer的dataflow服务
  116. query();
  117. function query() {
  118. var param = new SuperMap.QueryBySQLParameters({
  119. queryParams: {
  120. name: "Main_Road_L@China#1",
  121. attributeFilter: "SMID = 1755"
  122. }
  123. });
  124. new ol.supermap.QueryService(urlQuery).queryBySQL(param, function(serviceResult) {
  125. featureResult = serviceResult;
  126. dataFlowBroadcast = new ol.supermap.DataFlowService(urlDataFlow).initBroadcast();
  127. dataFlowBroadcast.on("broadcastSocketConnected", function(e) {
  128. timer = window.setInterval("broadcast()", 2000);
  129. });
  130. map.addLayer(resultLayer);
  131. });
  132. }
  133. });
  134. var count = 200;
  135. function broadcast() {
  136. if (count >= featureResult.result.recordsets[0].features.features[0].geometry.coordinates.length) {
  137. window.clearInterval(timer);
  138. return;
  139. }
  140. var point = featureResult.result.recordsets[0].features.features[0].geometry.coordinates[count];
  141. var feature = {
  142. geometry: {
  143. coordinates: [point[0], point[1]],
  144. type: "Point"
  145. },
  146. type: "Feature",
  147. properties: {
  148. id: 1,
  149. time: new Date()
  150. }
  151. };
  152. dataFlowBroadcast.broadcast(feature);
  153. count += 3;
  154. }
  155. //模拟实时数据 end
  156. </script>
  157. </body>
  158. </html>