components_labeltheme_react.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_componentsLabelTheme_React"></title>
  9. <script type="text/javascript" include="react" src="../js/include-web.js"></script>
  10. <script include="antd,iclient-mapboxgl-react,mapbox-gl-enhance" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
  11. <script type="text/javascript" src="../data/themeLableData.js"></script>
  12. <style>
  13. html,
  14. body {
  15. height: 100%;
  16. margin: 0;
  17. padding: 0;
  18. }
  19. #main {
  20. height: 100%;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="main"></div>
  26. <script type="text/babel">
  27. var SmWebMap = SuperMap.Components.SmWebMap;
  28. var SmLabelThemeLayer = SuperMap.Components.SmLabelThemeLayer;
  29. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  30. var features = [];
  31. var feat, popup, map;
  32. for (var i = 0; i < themeData.length; i++) {
  33. var lonlat = themeData[i].lonLat.split(',');
  34. var lng = parseFloat(lonlat[0]);
  35. var lat = parseFloat(lonlat[1]);
  36. var text = themeData[i].aqi;
  37. feat = new mapboxgl.supermap.ThemeFeature([lng, lat, text], themeData[i]);
  38. features.push(feat);
  39. }
  40. var mapOptions = {
  41. container: 'map',
  42. style: {
  43. version: 8,
  44. sources: {
  45. 'raster-tiles': {
  46. type: 'raster',
  47. tiles: [host + '/iserver/services/map-china400/rest/maps/China/zxyTileImage.png?z={z}&x={x}&y={y}'],
  48. tileSize: 256
  49. }
  50. },
  51. layers: [
  52. {
  53. id: 'simple-tiles',
  54. type: 'raster',
  55. source: 'raster-tiles',
  56. minzoom: 0,
  57. maxzoom: 22
  58. }
  59. ]
  60. },
  61. center: [116.4, 39.79],
  62. zoom: 3
  63. };
  64. var themeOptions = {
  65. // map: map,//该可选参数将在下个版本遗弃
  66. style: new SuperMap.ThemeStyle({
  67. labelRect: true,
  68. fontColor: '#000000',
  69. fontWeight: 'bolder',
  70. fontSize: '18px',
  71. fill: true,
  72. fillColor: '#FFFFFF',
  73. fillOpacity: 1,
  74. stroke: false,
  75. strokeColor: '#8B7B8B'
  76. }),
  77. themeField: 'aqi',
  78. styleGroups: [
  79. {
  80. start: 0,
  81. end: 51,
  82. style: {
  83. fillColor: '#6ACD06',
  84. fontSize: '17px'
  85. }
  86. },
  87. {
  88. start: 51,
  89. end: 101,
  90. style: {
  91. fillColor: '#FBD12A',
  92. fontSize: '19px'
  93. }
  94. },
  95. {
  96. start: 101,
  97. end: 151,
  98. style: {
  99. fillColor: '#FE8800',
  100. fontSize: '22px'
  101. }
  102. },
  103. {
  104. start: 151,
  105. end: 201,
  106. style: {
  107. fillColor: '#FF0000',
  108. fontSize: '24px'
  109. }
  110. },
  111. {
  112. start: 201,
  113. end: 301,
  114. style: {
  115. fillColor: '#CC0000',
  116. fontSize: '26px'
  117. }
  118. },
  119. {
  120. start: 301,
  121. end: 601,
  122. style: {
  123. fillColor: '#960453',
  124. fontSize: '28px'
  125. }
  126. }
  127. ]
  128. };
  129. function updateInfoView(feature, themeLayer) {
  130. if (!feature && popup) {
  131. removePopup();
  132. return;
  133. }
  134. if (!popup) {
  135. popup = new mapboxgl.Popup(({ maxWidth: 600 }));
  136. }
  137. var statisticsData = getStatisticsData();
  138. var contentHTML =
  139. '<table><tbody><tr>' +
  140. '<td><div>' +
  141. '<table><tbody><tr>' +
  142. "<td><div id='contentID' style='margin-top: 2px;margin-bottom: 2px;width:65px;height:60px;line-height:60px;text-align: center;font-size:35px;color: #ffffff;'>" +
  143. feature.attributes.aqi +
  144. '</div></td>' +
  145. "<td style='padding-right: 20px;'></td>" +
  146. '<td>' +
  147. "<div id='textID' style='text-align: left;font-size: 19px;text-shadow: 1px 1px 0 #ffffff;'>" +
  148. feature.attributes.quality +
  149. '</div>' +
  150. "<div style='font-size:10px;width:150px;'>" +
  151. feature.attributes.time_point +
  152. '</div>' +
  153. "<div style=' font-weight:lighter; font-size:14px;'>" +
  154. resources.text_currentCity +
  155. ':' +
  156. feature.attributes.area +
  157. '</div>' +
  158. '</td>' +
  159. '</tr> </tbody></table>' +
  160. "<table style='width:100%;font-size: 10px;border: 0 solid black;padding: 0;margin: 0;border-spacing: 0;'>" +
  161. '<tbody>' +
  162. '<tr>' +
  163. "<td style='font-weight:bold;width:45px;'></td><td style='font-weight:bold;width:51px;' align='center' nowrap='true'>Current</td>" +
  164. "<td style=' font-weight:bold;width:49px;' align='center' nowrap='true'>Min</td>" +
  165. "<td style='font-weight:bold;width:49px;' align='center' nowrap='true'>Max</td> " +
  166. '</tr>' +
  167. "<tr style='height: 23px;'><td style='padding-left: 0;padding-right: 0'><div style='width:46px;'><span style='font-weight:bold;'>PM2.5</span></div></td>" +
  168. "<td style='font-size:11px;' align='center'>" +
  169. feature.attributes.pm2_5 +
  170. '</td>' +
  171. "<td style='color:#0086c8;font-size:11px;' align='center'>" +
  172. statisticsData.minNum +
  173. '</td>' +
  174. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" +
  175. statisticsData.maxNum +
  176. '</td>' +
  177. '</tr>' +
  178. "<tr style='height: 23px;'><td style='padding-left: 0;padding-right: 0'><div style='width:46px;'><span style='font-weight:bold;'>PM10</span></div></td>" +
  179. "<td style='font-size:11px;' align='center'>" +
  180. feature.attributes.pm10 +
  181. '</td>' +
  182. "<td style='color:#0086c8;font-size:11px;' align='center'>" +
  183. statisticsData.minpm10 +
  184. '</td>' +
  185. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" +
  186. statisticsData.maxpm10 +
  187. '</td>' +
  188. '</tr>' +
  189. "<tr style='height:23px;'><td style='padding-left: 0;padding-right: 0'><div style='width:46px;'><span style='font-weight:bold;'>O3</span></div></td>" +
  190. "<td style='font-size:11px;' align='center'>" +
  191. feature.attributes.o3 +
  192. '</td>' +
  193. "<td style='color:#0086c8;font-size:11px;' align='center'>" +
  194. statisticsData.minO3 +
  195. '</td>' +
  196. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" +
  197. statisticsData.maxO3 +
  198. '</td></td>' +
  199. '</tr>' +
  200. "<tr style='height: 23px;'><td style='padding-left: 0;padding-right: 0'><div style='width:46px;'><span style='font-weight:bold;'>SO2</span></div></td>" +
  201. "<td style='font-size:11px;' align='center'>" +
  202. feature.attributes.so2 +
  203. '</td>' +
  204. "<td style='color:#0086c8;font-size:11px;' align='center'>" +
  205. statisticsData.minSO2 +
  206. '</td>' +
  207. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" +
  208. statisticsData.maxSO2 +
  209. '</td></td>' +
  210. '</tr>' +
  211. "<tr style='height: 23px;'><td style='padding-left: 0;padding-right: 0'><div style='width:46px;'><span style='font-weight:bold;'>NO2</span></div></td>" +
  212. "<td style='font-size:11px;' align='center'>" +
  213. feature.attributes.no2 +
  214. '</td>' +
  215. "<td style='color:#0086c8;font-size:11px;' align='center'>" +
  216. statisticsData.minNO2 +
  217. '</td>' +
  218. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" +
  219. statisticsData.maxNO2 +
  220. '</td></td>' +
  221. '</tr></tbody> </table></div></td></tr></tbody> </table>';
  222. var latLng = getLatLng(feature.attributes.lonLat);
  223. popup
  224. .setLngLat(latLng)
  225. .setHTML(contentHTML)
  226. .addTo(map);
  227. //设置弹框内容颜色
  228. var groups = themeLayer.styleGroups;
  229. for (var i = 0; i < groups.length; i++) {
  230. if (feature.attributes.aqi >= groups[0].start && feature.attributes.aqi < groups[0].end) {
  231. setColor('#6ACD06');
  232. } else if (feature.attributes.aqi >= groups[1].start && feature.attributes.aqi < groups[1].end) {
  233. setColor('#FBD12A');
  234. } else if (feature.attributes.aqi >= groups[2].start && feature.attributes.aqi < groups[2].end) {
  235. setColor('#FE8800');
  236. } else if (feature.attributes.aqi >= groups[3].start && feature.attributes.aqi < groups[3].end) {
  237. setColor('#FF0000');
  238. } else if (feature.attributes.aqi >= groups[4].start && feature.attributes.aqi < groups[4].end) {
  239. setColor('#CC0000');
  240. } else if (feature.attributes.aqi >= groups[5].start && feature.attributes.aqi < groups[5].end) {
  241. setColor('#960453');
  242. }
  243. }
  244. }
  245. function getStatisticsData() {
  246. if (statisticsData) {
  247. return statisticsData;
  248. }
  249. //遍历数组,获取单个属性。组成新的数组
  250. var pm25 = [],
  251. pm10s = [],
  252. o3s = [],
  253. so2s = [],
  254. no2s = [];
  255. for (var i = 0; i < themeData.length; i++) {
  256. pm10s.push(themeData[i].pm2_5);
  257. pm25.push(themeData[i].pm10);
  258. o3s.push(themeData[i].o3);
  259. so2s.push(themeData[i].so2);
  260. no2s.push(themeData[i].no2);
  261. }
  262. //获取单个属性的最大最小值
  263. var statisticsData = {
  264. maxNum: Math.max.apply(Math, pm25),
  265. minNum: Math.min.apply(Math, pm25),
  266. maxpm10: Math.max.apply(Math, pm10s),
  267. minpm10: Math.min.apply(Math, pm10s),
  268. maxO3: Math.max.apply(Math, o3s),
  269. minO3: Math.min.apply(Math, o3s),
  270. maxSO2: Math.max.apply(Math, so2s),
  271. minSO2: Math.min.apply(Math, so2s),
  272. maxNO2: Math.max.apply(Math, no2s),
  273. minNO2: Math.min.apply(Math, no2s)
  274. };
  275. return statisticsData;
  276. }
  277. function setColor(color) {
  278. document.getElementById('contentID').style.backgroundColor = color;
  279. document.getElementById('textID').style.color = color;
  280. }
  281. function getLatLng(latLng) {
  282. var latLng = latLng.split(',');
  283. return [parseFloat(latLng[0]), parseFloat(latLng[1])];
  284. }
  285. function handleMouseOver(params) {
  286. var e = params.mapboxEvent;
  287. map = params.map;
  288. var themeLayer = map.getLayer(params.layerId);
  289. if (e.target && e.target.refDataID) {
  290. var fid = e.target.refDataID;
  291. var fea = themeLayer.getFeatureById(fid);
  292. if (fea) {
  293. updateInfoView(fea, themeLayer);
  294. }
  295. } else {
  296. removePopup();
  297. }
  298. }
  299. function removePopup() {
  300. popup && popup.remove(map);
  301. }
  302. ReactDOM.render(
  303. <SmWebMap mapOptions={mapOptions}>
  304. <SmLabelThemeLayer
  305. options={themeOptions}
  306. layerName={'LabelThemeLayer'}
  307. data={features}
  308. onMousemove={handleMouseOver}
  309. />
  310. </SmWebMap>,
  311. document.getElementById('main')
  312. );
  313. </script>
  314. </body>
  315. </html>