dataFlowService_mapv.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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" include="widgets" src="../js/include-web.js"></script>
  10. </head>
  11. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  12. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  13. <script type="text/javascript" include='mapv' src="../../dist/leaflet/include-leaflet.js"></script>
  14. <script type="text/javascript">
  15. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  16. var wsHost = "wss:\//" + (window.isLocal ? document.location.hostname + ":8800" : "iclsvrws.supermap.io");
  17. var map,
  18. urlMap = host + "/iserver/services/map-china400/rest/maps/ChinaDark",
  19. urlQuery = host + "/iserver/services/map-china400/rest/maps/China_4326",
  20. urlDataFlow = wsHost + "/iserver/services/dataflowTest2/dataflow";
  21. map = L.map('map', {
  22. preferCanvas: true,
  23. crs: L.CRS.EPSG3857,
  24. center: [37, 108.42],
  25. maxZoom: 18,
  26. zoom: 5
  27. });
  28. L.supermap.tiledMapLayer(urlMap).addTo(map);
  29. SuperMap.SecurityManager.registerToken(urlDataFlow, window.exampleToken);
  30. var popup = L.popup({
  31. offset: L.point(0, 0),
  32. autoPan: true
  33. });
  34. //创建DataFlowLayer,创建DataFlowLayer订阅iServer dataflow服务并将结果加载到地图上
  35. var dataFlowLayer = L.supermap.dataFlowLayer(urlDataFlow, {
  36. render: 'mapv',
  37. pointToLayer: function (geoJsonPoint, latlng) {
  38. return L.marker(latlng, {
  39. icon: L.icon({
  40. iconUrl: '../img/taxi.png',
  41. iconAnchor: [16, 16]
  42. })
  43. });
  44. },
  45. onEachFeature: function (feature, layer) {
  46. popup.setLatLng(L.GeoJSON.coordsToLatLng(feature.geometry.coordinates))
  47. .setContent(feature.properties.time);
  48. if (!popup.isOpen()) {
  49. popup.addTo(map);
  50. }
  51. },
  52. });
  53. dataFlowLayer.addTo(map);
  54. //模拟实时数据 start
  55. //查询expectCount个线数据,每两秒将expectCount个点通过dataFlowService广播给iSevrer的dataflow服务,模拟expectCount个小汽车的移动轨迹
  56. //该测试数据有21331个数据
  57. var expectCount = 10000;
  58. query();
  59. var timer, featureResult, dataFlowBroadcast;
  60. function query() {
  61. widgets.loader.showLoader();
  62. var ids = [];
  63. while (ids.length < expectCount) {
  64. var n = Math.round(Math.random() * 21331);
  65. if (ids.indexOf(n) === -1) {
  66. ids.push(n);
  67. }
  68. }
  69. var str = "(" + ids.join() + ")";
  70. var param = new SuperMap.QueryBySQLParameters({
  71. expectCount: expectCount,
  72. queryParams: {
  73. name: "County_Road_ln@China",
  74. attributeFilter: "SMID in " + str,
  75. }
  76. });
  77. L.supermap
  78. .queryService(urlQuery)
  79. .queryBySQL(param, function (serviceResult) {
  80. featureResult = serviceResult;
  81. dataFlowBroadcast = L.supermap.dataFlowService(urlDataFlow).initBroadcast();
  82. dataFlowBroadcast.on('broadcastSocketConnected', function (e) {
  83. widgets.loader.removeLoader();
  84. broadcast();
  85. timer = window.setInterval("broadcast()", 2000);
  86. })
  87. });
  88. }
  89. var count = 0;
  90. function broadcast() {
  91. var features = [];
  92. for (var index = 0; index < featureResult.result.recordsets[0].features.features.length; index++) {
  93. var geometry = featureResult.result.recordsets[0].features.features[index].geometry;
  94. var point = geometry.coordinates[(count % geometry.coordinates.length)];
  95. //处理多线
  96. if (L.Util.isArray(point[0])) {
  97. point = point[0];
  98. }
  99. var data = {
  100. geometry: {
  101. coordinates: [point[0], point[1]],
  102. type: "Point"
  103. },
  104. type: "Feature",
  105. properties: {
  106. id: index + 1,
  107. time: new Date()
  108. }
  109. };
  110. features.push(data);
  111. }
  112. dataFlowBroadcast.broadcast(features);
  113. count += 2;
  114. }
  115. //模拟实时数据 end
  116. </script>
  117. </body>
  118. </html>