labelThemeLayer.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en-US">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  9. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  10. <title data-i18n="resources.title_themeLabel"></title>
  11. <style>
  12. .legend td {
  13. width: 60px;
  14. height: 12px;
  15. font-size: 12px;
  16. }
  17. .leaflet-popup-content {
  18. margin: 5px
  19. }
  20. .mapboxgl-popup-content {
  21. box-shadow: 0 3px 14px rgba(0, 0, 0, 0.4) !important;
  22. }
  23. </style>
  24. <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
  25. <script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
  26. <script type="text/javascript" src="../data/themeLableData.js"></script>
  27. </head>
  28. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  29. <div id="map" style="margin:0 auto;width: 100%;height: 100%">
  30. </div>
  31. <script type="text/javascript">
  32. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  33. baseUrl = host + "/iserver/services/map-china400/rest/maps/China";
  34. var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
  35. " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
  36. " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
  37. var themeLayer, popup;
  38. var map = new mapboxgl.Map({
  39. container: 'map',
  40. style: {
  41. "version": 8,
  42. "sources": {
  43. "raster-tiles": {
  44. "attribution": attribution,
  45. "type": "raster",
  46. "tiles": [baseUrl + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
  47. "tileSize": 256,
  48. },
  49. },
  50. "layers": [{
  51. "id": "simple-tiles",
  52. "type": "raster",
  53. "source": "raster-tiles",
  54. "minzoom": 0,
  55. "maxzoom": 22
  56. }]
  57. },
  58. center: [116.40, 39.79],
  59. zoom: 3
  60. });
  61. map.addControl(new mapboxgl.NavigationControl(), 'top-left');
  62. map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
  63. initThemeLayer();
  64. initLegendView();
  65. function initThemeLayer() {
  66. themeLayer = new mapboxgl.supermap.LabelThemeLayer("labelThemeLayer", {
  67. // map: map,//该可选参数将在下个版本遗弃
  68. attributions: " ",
  69. style: new SuperMap.ThemeStyle({
  70. labelRect: true,
  71. fontColor: "#000000",
  72. fontWeight: "bolder",
  73. fontSize: "18px",
  74. fill: true,
  75. fillColor: "#FFFFFF",
  76. fillOpacity: 1,
  77. stroke: false,
  78. strokeColor: "#8B7B8B"
  79. }),
  80. themeField: "aqi",
  81. styleGroups: [
  82. {
  83. start: 0,
  84. end: 51,
  85. style: {
  86. fillColor: "#6ACD06",
  87. fontSize: "17px"
  88. }
  89. }, {
  90. start: 51,
  91. end: 101,
  92. style: {
  93. fillColor: "#FBD12A",
  94. fontSize: "19px"
  95. }
  96. }, {
  97. start: 101,
  98. end: 151,
  99. style: {
  100. fillColor: "#FE8800",
  101. fontSize: "22px"
  102. }
  103. }, {
  104. start: 151,
  105. end: 201,
  106. style: {
  107. fillColor: "#FF0000",
  108. fontSize: "24px"
  109. }
  110. }, {
  111. start: 201,
  112. end: 301,
  113. style: {
  114. fillColor: "#CC0000",
  115. fontSize: "26px"
  116. }
  117. }, {
  118. start: 301,
  119. end: 601,
  120. style: {
  121. fillColor: "#960453",
  122. fontSize: "28px"
  123. }
  124. }
  125. ]
  126. });
  127. map.addLayer(themeLayer);
  128. //专题图层 mousemove 事件
  129. themeLayer.on('mousemove', handleMouseOver);
  130. addFeatures();
  131. }
  132. function addFeatures() {
  133. var labelFeatures = [];
  134. var feat;
  135. for (var i = 0; i < themeData.length; i++) {
  136. var lonlat = themeData[i].lonLat.split(",");
  137. var lng = parseFloat(lonlat[0]);
  138. var lat = parseFloat(lonlat[1]);
  139. var text = themeData[i].aqi;
  140. feat = new mapboxgl.supermap.ThemeFeature([lng, lat, text], themeData[i]);
  141. labelFeatures.push(feat);
  142. }
  143. themeLayer.addFeatures(labelFeatures);
  144. }
  145. //图例
  146. function initLegendView() {
  147. var LegendControl = function () {
  148. };
  149. LegendControl.prototype.onAdd = function () {
  150. this._div = document.createElement('div');
  151. this._div.className = 'mapboxgl-ctrl panel legend';
  152. this._div.innerHTML = "<div class='panel-body'><table>" +
  153. "<tr class='text-center'><td>" + resources.text_great + "</td><td >" + resources.text_good + "</td> <td >" + resources.text_mildPollution + "</td><td>" + resources.text_moderatePollution + "</td><td >" + resources.text_heavyPollution + "</td><td >" + resources.text_severePollution + "</td></tr>" +
  154. "<tr class='text-center'>" +
  155. "<td style='background: #6ACD06'></td><td style='background: #FBD12A'></td><td style='background: #FE8800'></td>" +
  156. "<td style='background: #FF0000'></td><td style='background: #CC0000'></td><td style='background: #960453'></td>" +
  157. "</tr>" +
  158. "<tr class='text-center'><td>0-51</td><td >51-101</td><td>101-151</td><td>151-201</td><td>201-301</td><td>301-601</td></tr>" +
  159. "</table></div>";
  160. return this._div;
  161. };
  162. map.addControl(new LegendControl(), 'bottom-left');
  163. }
  164. //图层信息框
  165. function updateInfoView(feature) {
  166. if (!feature && popup) {
  167. removePopup();
  168. return;
  169. }
  170. if (!popup) {
  171. popup = new mapboxgl.Popup({maxWidth: 'none'});
  172. }
  173. var statisticsData = getStatisticsData();
  174. var contentHTML = "<table><tbody><tr>" +
  175. "<td><div style='margin-left: 15px'>" +
  176. "<table><tbody><tr>" +
  177. "<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;'>" + feature.attributes.aqi + "</div></td>" +
  178. "<td style='padding-right: 20px;'></td>" +
  179. "<td>" +
  180. "<div id='textID' style='text-align: left;font-size: 19px;text-shadow: 1px 1px 0 #ffffff;'>" + feature.attributes.quality + "</div>" +
  181. "<div style='font-size:10px;width:150px;'>" + feature.attributes.time_point + "</div>" +
  182. "<div style=' font-weight:lighter; font-size:14px;'>" + resources.text_currentCity + ":" + feature.attributes.area + "</div>" +
  183. "</td>" +
  184. "</tr> </tbody></table>" +
  185. "<table style='width:100%;font-size: 10px;border: 0 solid black;padding: 0;margin: 0;border-spacing: 0;'>" +
  186. "<tbody>" +
  187. "<tr>" +
  188. "<td style='font-weight:bold;width:45px;'></td><td style='font-weight:bold;width:51px;' align='center' nowrap='true'>Current</td>" +
  189. "<td style=' font-weight:bold;width:49px;' align='center' nowrap='true'>Min</td>" +
  190. "<td style='font-weight:bold;width:49px;' align='center' nowrap='true'>Max</td> " +
  191. "</tr>" +
  192. "<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>" +
  193. "<td style='font-size:11px;' align='center'>" + feature.attributes.pm2_5 + "</td>" +
  194. "<td style='color:#0086c8;font-size:11px;' align='center'>" + statisticsData.minNum + "</td>" +
  195. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" + statisticsData.maxNum + "</td>" +
  196. "</tr>" +
  197. "<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>" +
  198. "<td style='font-size:11px;' align='center'>" + feature.attributes.pm10 + "</td>" +
  199. "<td style='color:#0086c8;font-size:11px;' align='center'>" + statisticsData.minpm10 + "</td>" +
  200. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" + statisticsData.maxpm10 + "</td>" +
  201. "</tr>" +
  202. "<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>" +
  203. "<td style='font-size:11px;' align='center'>" + feature.attributes.o3 + "</td>" +
  204. "<td style='color:#0086c8;font-size:11px;' align='center'>" + statisticsData.minO3 + "</td>" +
  205. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" + statisticsData.maxO3 + "</td></td>" +
  206. "</tr>" +
  207. "<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>" +
  208. "<td style='font-size:11px;' align='center'>" + feature.attributes.so2 + "</td>" +
  209. "<td style='color:#0086c8;font-size:11px;' align='center'>" + statisticsData.minSO2 + "</td>" +
  210. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" + statisticsData.maxSO2 + "</td></td>" +
  211. "</tr>" +
  212. "<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>" +
  213. "<td style='font-size:11px;' align='center'>" + feature.attributes.no2 + "</td>" +
  214. "<td style='color:#0086c8;font-size:11px;' align='center'>" + statisticsData.minNO2 + "</td>" +
  215. "<td style='color:#ce3c3a;font-size:11px;' align='center'>" + statisticsData.maxNO2 + "</td></td>" +
  216. "</tr></tbody> </table></div></td></tr></tbody> </table>";
  217. var latLng = getLatLng(feature.attributes.lonLat);
  218. popup.setLngLat(latLng).setHTML(contentHTML).addTo(map);
  219. //设置弹框内容颜色
  220. var groups = themeLayer.styleGroups;
  221. for (var i = 0; i < groups.length; i++) {
  222. if (feature.attributes.aqi >= groups[0].start && feature.attributes.aqi < groups[0].end) {
  223. setColor("#6ACD06");
  224. }
  225. else if (feature.attributes.aqi >= groups[1].start && feature.attributes.aqi < groups[1].end) {
  226. setColor("#FBD12A");
  227. }
  228. else if (feature.attributes.aqi >= groups[2].start && feature.attributes.aqi < groups[2].end) {
  229. setColor("#FE8800");
  230. }
  231. else if (feature.attributes.aqi >= groups[3].start && feature.attributes.aqi < groups[3].end) {
  232. setColor("#FF0000");
  233. }
  234. else if (feature.attributes.aqi >= groups[4].start && feature.attributes.aqi < groups[4].end) {
  235. setColor("#CC0000");
  236. }
  237. else if (feature.attributes.aqi >= groups[5].start && feature.attributes.aqi < groups[5].end) {
  238. setColor("#960453");
  239. }
  240. }
  241. function setColor(color) {
  242. document.getElementById("contentID").style.backgroundColor = color;
  243. document.getElementById("textID").style.color = color;
  244. }
  245. function getLatLng(latLng) {
  246. var latLng = latLng.split(",");
  247. return [parseFloat(latLng[0]), parseFloat(latLng[1])]
  248. }
  249. }
  250. function getStatisticsData() {
  251. if (this.statisticsData) {
  252. return this.statisticsData;
  253. }
  254. //遍历数组,获取单个属性。组成新的数组
  255. var pm25 = [], pm10s = [], o3s = [], so2s = [], no2s = [];
  256. for (var i = 0; i < themeData.length; i++) {
  257. pm10s.push(themeData[i].pm2_5);
  258. pm25.push(themeData[i].pm10);
  259. o3s.push(themeData[i].o3);
  260. so2s.push(themeData[i].so2);
  261. no2s.push(themeData[i].no2);
  262. }
  263. //获取单个属性的最大最小值
  264. this.statisticsData = {
  265. maxNum: Math.max.apply(Math, pm25),
  266. minNum: Math.min.apply(Math, pm25),
  267. maxpm10: Math.max.apply(Math, pm10s),
  268. minpm10: Math.min.apply(Math, pm10s),
  269. maxO3: Math.max.apply(Math, o3s),
  270. minO3: Math.min.apply(Math, o3s),
  271. maxSO2: Math.max.apply(Math, so2s),
  272. minSO2: Math.min.apply(Math, so2s),
  273. maxNO2: Math.max.apply(Math, no2s),
  274. minNO2: Math.min.apply(Math, no2s)
  275. };
  276. return this.statisticsData;
  277. }
  278. function handleMouseOver(e) {
  279. if (e.target && e.target.refDataID) {
  280. var fid = e.target.refDataID;
  281. var fea = themeLayer.getFeatureById(fid);
  282. if (fea) {
  283. updateInfoView(fea);
  284. }
  285. } else {
  286. removePopup();
  287. }
  288. }
  289. function removePopup() {
  290. popup && popup.remove();
  291. }
  292. </script>
  293. </body>
  294. </html>