dataFlowService.html 6.0 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. <style>
  11. .mapboxgl-popup {
  12. max-width: 200px;
  13. }
  14. </style>
  15. </head>
  16. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  17. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  18. <script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
  19. <script>
  20. var host = (window.isLocal ? window.server : "https://iserver.supermap.io"),
  21. tileUrl = host +
  22. '/iserver/services/map-china400/rest/maps/China/zxyTileImage.png?prjCoordSys='+encodeURIComponent('{"epsgCode":3857}')+'&z={z}&x={x}&y={y}',
  23. urlQuery = host + "/iserver/services/map-china400/rest/maps/China_4326";
  24. wsHost = "wss:\//" + (window.isLocal ? document.location.hostname + ":8800" : "iclsvrws.supermap.io");
  25. var urlDataFlow = wsHost + "/iserver/services/dataflowTest/dataflow";
  26. var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
  27. " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
  28. " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
  29. var dataFlowService;
  30. var map = new mapboxgl.Map({
  31. container: 'map',
  32. style: {
  33. "version": 8,
  34. "sources": {
  35. "raster-tiles": {
  36. "attribution": attribution,
  37. "type": "raster",
  38. "tiles": [tileUrl],
  39. "tileSize": 256,
  40. },
  41. },
  42. "layers": [{
  43. "id": "simple-tiles",
  44. "type": "raster",
  45. "source": "raster-tiles",
  46. "minzoom": 0,
  47. "maxzoom": 18
  48. }],
  49. },
  50. center: [116.443571, 39.887549],
  51. maxZoom: 18,
  52. zoom: 10
  53. });
  54. map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
  55. map.addControl(new mapboxgl.NavigationControl(), 'top-left');
  56. SuperMap.SecurityManager.registerToken(urlDataFlow, window.exampleToken);
  57. var popup = new mapboxgl.Popup();
  58. map.on('load', function () {
  59. var options = {
  60. ws: urlDataFlow
  61. };
  62. var dataFlowSubscribe = new mapboxgl.supermap.DataFlowService(options.ws, {
  63. geometry: options.geometry,
  64. prjCoordSys: options.prjCoordSys,
  65. excludeField: options.excludeField
  66. }).initSubscribe();
  67. dataFlowSubscribe.on('messageSucceeded', function (msg) {
  68. popup.remove();
  69. addLayer(msg);
  70. });
  71. query();
  72. });
  73. //模拟实时数据 start
  74. //查询一个线数据,每两秒将一个点通过dataFlowService广播给iSevrer的dataflow服务
  75. function query() {
  76. var param = new SuperMap.QueryBySQLParameters({
  77. queryParams: {
  78. name: "Main_Road_L@China#1",
  79. attributeFilter: "SMID = 1755"
  80. }
  81. });
  82. queryService = new mapboxgl.supermap.QueryService(urlQuery).queryBySQL(param, function (serviceResult) {
  83. featureResult = serviceResult;
  84. dataFlowBroadcast = new mapboxgl.supermap.DataFlowService(urlDataFlow).initBroadcast();
  85. dataFlowBroadcast.on('broadcastSocketConnected', function (e) {
  86. timer = window.setInterval("broadcast()", 2000);
  87. });
  88. });
  89. }
  90. var count = 200;
  91. function broadcast() {
  92. if (count >= featureResult.result.recordsets[0].features.features[0].geometry.coordinates.length) {
  93. window.clearInterval(timer);
  94. return;
  95. }
  96. var point = featureResult.result.recordsets[0].features.features[0].geometry.coordinates[count];
  97. var feature = {
  98. geometry: {
  99. coordinates: [point[0], point[1]],
  100. type: "Point"
  101. },
  102. type: "Feature",
  103. properties: {
  104. id: 1,
  105. time: new Date()
  106. }
  107. };
  108. dataFlowBroadcast.broadcast(feature);
  109. count += 3;
  110. }
  111. function addLayer(msg) {
  112. if (!msg.featureResult) {
  113. return;
  114. }
  115. var feature = msg.featureResult;
  116. var coord = feature.geometry.coordinates;
  117. var data = {
  118. geometry: {
  119. type: 'Point',
  120. coordinates: coord,
  121. },
  122. type: "Feature"
  123. };
  124. if (!map.getSource('location')) {
  125. map.addSource('location', {
  126. 'type': 'geojson',
  127. 'data': data
  128. });
  129. map.addLayer({
  130. "id": "point",
  131. "type": "circle",
  132. "paint": {
  133. "circle-radius": 6,
  134. "circle-color": 'red',
  135. },
  136. "source": 'location'
  137. });
  138. }
  139. map.getSource('location').setData(data);
  140. popup.setLngLat(coord)
  141. .setHTML(feature.properties.time)
  142. .addTo(map);
  143. }
  144. //模拟实时数据 end
  145. </script>
  146. </body>
  147. </html>