vizLayer_heatGridLayer.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_heatGridMapLayer"></title>
  9. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  10. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  11. <style type="text/css">
  12. body {
  13. margin: 0;
  14. overflow: hidden;
  15. background: #fff;
  16. width: 100%;
  17. height: 100%
  18. }
  19. #map {
  20. position: absolute;
  21. width: 100%;
  22. height: 100%;
  23. }
  24. #toolbar {
  25. position: absolute;
  26. top: 50px;
  27. right: 10px;
  28. width: 300px;
  29. text-align: center;
  30. z-index: 100;
  31. border-radius: 4px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="map"></div>
  37. <script>
  38. var map, layer, heatGridLayer, selectGrid, infowin;
  39. //定义网格的styles
  40. var items = [
  41. {
  42. start: 0,
  43. end: 2,
  44. style: {
  45. strokeColor: "#C69944",
  46. strokeWidth: 1,
  47. fillColor: "#B8E4B8",
  48. fillOpacity: 0.5
  49. }
  50. },
  51. {
  52. start: 2,
  53. end: 4,
  54. style: {
  55. strokeColor: "#C69944",
  56. strokeWidth: 1,
  57. fillColor: "#66dd66",
  58. fillOpacity: 0.5
  59. }
  60. },
  61. {
  62. start: 4,
  63. end: 6,
  64. style: {
  65. strokeColor: "#C69944",
  66. strokeWidth: 1,
  67. fillColor: "#00ee00",
  68. fillOpacity: 0.5
  69. }
  70. },
  71. {
  72. start: 6,
  73. end: 8,
  74. style: {
  75. strokeColor: "#C69944",
  76. strokeWidth: 1,
  77. fillColor: "#008800",
  78. fillOpacity: 0.5
  79. }
  80. },
  81. {
  82. start: 8,
  83. end: 10,
  84. style: {
  85. strokeColor: "#C69944",
  86. strokeWidth: 1,
  87. fillColor: "#df8505",
  88. fillOpacity: 0.5
  89. }
  90. },
  91. {
  92. start: 10,
  93. end: 12,
  94. style: {
  95. strokeColor: "#C69944",
  96. strokeWidth: 1,
  97. fillColor: "#CC9933",
  98. fillOpacity: 0.5
  99. }
  100. },
  101. {
  102. start: 12,
  103. end: 16,
  104. style: {
  105. strokeColor: "#C69944",
  106. strokeWidth: 1,
  107. fillColor: "#FF0000",
  108. fillOpacity: 0.5
  109. }
  110. }
  111. ];
  112. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  113. var url = host + "/iserver/services/map-world/rest/maps/World";
  114. //初始化
  115. init();
  116. function init() {
  117. map = new SuperMap.Map("map", {
  118. controls: [
  119. new SuperMap.Control.ScaleLine(),
  120. new SuperMap.Control.Zoom(),
  121. new SuperMap.Control.LayerSwitcher(),
  122. new SuperMap.Control.Navigation({
  123. dragPanOptions: {
  124. enableKinetic: true
  125. }
  126. })]
  127. });
  128. map.addControl(new SuperMap.Control.MousePosition());
  129. layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
  130. transparent: true,
  131. cacheEnabled: true
  132. }, {maxResolution: "auto"});
  133. //创建热点网格图
  134. heatGridLayer = new SuperMap.Layer.HeatGridLayer("heatGrid");
  135. layer.events.on({"layerInitialized": addLayer});
  136. }
  137. //添加图层、注册事件、激活格网选择控件
  138. function addLayer() {
  139. map.addLayers([layer, heatGridLayer]);
  140. map.setCenter(new SuperMap.LonLat(0, 0), 0);
  141. // 创建格网选择控件
  142. var select = new SuperMap.Control.SelectGrid(heatGridLayer, {
  143. callbacks: {
  144. //点击feature事件
  145. clickFeature: function (f) {
  146. closeInfoWin();
  147. openInfoWin(f);
  148. },
  149. clickout: function () {
  150. closeInfoWin();
  151. }
  152. }
  153. });
  154. map.events.on({
  155. "movestart": function () {
  156. closeInfoWin();
  157. }
  158. });
  159. map.addControl(select);
  160. select.activate();
  161. createHeatPoints();
  162. }
  163. //向服务器发送请求,获取数据
  164. function createHeatPoints() {
  165. var points = [new SuperMap.Geometry.Point(-180, 90),
  166. new SuperMap.Geometry.Point(180, 90),
  167. new SuperMap.Geometry.Point(180, -90),
  168. new SuperMap.Geometry.Point(-180, -90)
  169. ],
  170. linearRings = new SuperMap.Geometry.LinearRing(points),
  171. region = new SuperMap.Geometry.Polygon([linearRings]);
  172. var queryParam, queryByGeometryParameters, queryService;
  173. //设置查询数据集的查询过滤参数
  174. queryParam = new SuperMap.REST.FilterParameter({name: "Capitals@World.1"});
  175. //设置 Geometry查询的相关参数
  176. queryByGeometryParameters = new SuperMap.REST.QueryByGeometryParameters({
  177. queryParams: [queryParam],
  178. geometry: region,
  179. spatialQueryMode: SuperMap.REST.SpatialQueryMode.INTERSECT
  180. });
  181. queryService = new SuperMap.REST.QueryByGeometryService(url, {
  182. eventListeners: {
  183. "processCompleted": processCompleted,
  184. "processFailed": processFailed
  185. }
  186. });
  187. queryService.processAsync(queryByGeometryParameters);
  188. }
  189. function processCompleted(queryEventArgs) {
  190. var i, j, result = queryEventArgs.result;
  191. var heatFeatures = [];
  192. if (result && result.recordsets) {
  193. for (i = 0, recordsets = result.recordsets, len = recordsets.length; i < len; i++) {
  194. if (recordsets[i].features) {
  195. for (j = 0; j < recordsets[i].features.length; j++) {
  196. var feature = recordsets[i].features[j];
  197. var point = feature.geometry;
  198. if (point.CLASS_NAME == SuperMap.Geometry.Point.prototype.CLASS_NAME) {
  199. feature.attributes.temperature = parseInt(Math.random() * 45 * 10) / 10;
  200. feature.style = {
  201. pointRadius: 4,
  202. graphic: true,
  203. externalGraphic: "images/country.png",
  204. graphicWidth: 6,
  205. graphicHeight: 6
  206. };
  207. heatFeatures.push(feature);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. //设置地图在第4级的时候进行格网散开
  214. heatGridLayer.spreadZoom = 4;
  215. heatGridLayer.items = items;
  216. //设置label显示的数据为字段temperature的内容
  217. heatGridLayer.dataField = "temperature";
  218. heatGridLayer.addFeatures(heatFeatures);
  219. }
  220. function processFailed(e) {
  221. widgets.alert.showAlert(e.error.errorMsg,false);
  222. }
  223. //弹出信息框
  224. function openInfoWin(feature) {
  225. var geo = feature.geometry;
  226. var bounds = geo.getBounds();
  227. var center = bounds.getCenterLonLat();
  228. var contentHTML = "<div style='font-size:.8em; opacity: 0.8; overflow-y:hidden;'>";
  229. contentHTML += "<div>" + "SmID:" + feature.data.SmID + "<br />" + resources.text_country + feature.data.COUNTRY + "<br />" + resources.text_capital + feature.data.CAPITAL + "</div></div>";
  230. var popup = new SuperMap.Popup.FramedCloud("popwin",
  231. new SuperMap.LonLat(center.lon, center.lat),
  232. null,
  233. contentHTML,
  234. null,
  235. true);
  236. feature.popup = popup;
  237. infowin = popup;
  238. map.addPopup(popup);
  239. }
  240. //关闭信息框
  241. function closeInfoWin() {
  242. if (infowin) {
  243. try {
  244. infowin.hide();
  245. infowin.destroy();
  246. }
  247. catch (e) {
  248. }
  249. }
  250. }
  251. </script>
  252. </body>
  253. </html>