vizLayer_clusterLayer.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_clusterLayer"></title>
  9. <script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
  10. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  11. <script type="text/javascript" src="../data/changchundata.js"></script>
  12. <style type="text/css">
  13. body {
  14. margin: 0;
  15. overflow: hidden;
  16. background: #fff;
  17. width: 100%;
  18. height: 100%
  19. }
  20. #map {
  21. position: absolute;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. #toolbar {
  26. position: absolute;
  27. top: 50px;
  28. right: 10px;
  29. width: 300px;
  30. text-align: center;
  31. z-index: 100;
  32. border-radius: 4px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="map"></div>
  38. <script>
  39. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  40. var map, layer, clusterLayer, infowin,
  41. url = host + "/iserver/services/map-changchun/rest/maps/长春市区图";
  42. var styleLine = {
  43. strokeColor: "black",
  44. strokeWidth: 1,
  45. fill: false
  46. };
  47. init();
  48. function init() {
  49. //创建map对象和动态图层
  50. map = new SuperMap.Map("map", {
  51. controls: [
  52. new SuperMap.Control.ScaleLine(),
  53. new SuperMap.Control.Zoom(),
  54. new SuperMap.Control.Navigation({
  55. dragPanOptions: {
  56. enableKinetic: true
  57. }
  58. })], units: "m"
  59. });
  60. layer = new SuperMap.Layer.TiledDynamicRESTLayer("changchun", url, {
  61. transparent: true,
  62. cacheEnabled: true,
  63. }, {maxResolution: "auto"});
  64. layer.events.on({"layerInitialized": addLayer});
  65. }
  66. function addLayer() {
  67. //创建聚散图层并添加layers
  68. clusterLayer = new SuperMap.Layer.ClusterLayer("Cluster");
  69. map.addLayers([layer, clusterLayer]);
  70. //创建聚散选择控件。该控件实现了聚散图层的鼠标事件。
  71. var select = new SuperMap.Control.SelectCluster(clusterLayer, {
  72. callbacks: {
  73. click: function (f) { //点击兴趣点弹出信息窗口
  74. closeInfoWin();
  75. if (!f.isCluster) { //当点击聚散点的时候不弹出信息窗口
  76. openInfoWin(f);
  77. }
  78. },
  79. clickout: function () { //点击空白处关闭信息窗口
  80. closeInfoWin();
  81. }
  82. }
  83. });
  84. //将控件添加到map上
  85. map.addControl(select);
  86. //设置中心点,出图
  87. map.setCenter(new SuperMap.LonLat(4803, -3726), 1);
  88. clusterLayer.events.on({
  89. "moveend": function (e) {//注册moveend事件,当缩放的时候关闭信息窗口
  90. if (e && e.zoomChanged) closeInfoWin();
  91. }
  92. });
  93. clusterLayer.events.on({
  94. "clusterend": function (e) {
  95. //e包含了聚散完成所需要的信息,其结构如下e={clusterPoints:[],displayedPoints:[],element:null,object:null,type:"clusterEnd"}
  96. //其中,clusterMaps是包含了聚散点映射关系集合,clusterPoints[i]则表示第i个聚散点映射关系,其类型为{SuperMap.Feature.Vector},其内的children属性包含有对应的实际点坐标
  97. //而displayedPoints则是用户所设定的某一范围内不需要被聚散的点集合
  98. }
  99. });
  100. //激活控件。
  101. select.activate();
  102. //往聚散图层中添加兴趣点
  103. var fs1 = getFeatures();
  104. clusterLayer.addFeatures(fs1);
  105. }
  106. function getFeatures() {
  107. var b = map.getExtent();
  108. var ps = [];
  109. var fs = changchundata;
  110. for (var i = 0; i < fs.length; i++) {
  111. var fi = fs[i];
  112. var c = fi.geometry.center;
  113. var f = new SuperMap.Feature.Vector();
  114. f.geometry = new SuperMap.Geometry.Point(c.x, c.y);
  115. f.style = {
  116. pointRadius: 4,
  117. graphic: true,
  118. externalGraphic: "./images/cluster4.png",
  119. graphicWidth: 12,
  120. graphicHeight: 12
  121. };
  122. f.info = {
  123. "name": fi.fieldValues[4]
  124. };
  125. ps.push(f);
  126. }
  127. return ps;
  128. }
  129. function openInfoWin(feature) {
  130. var geo = feature.geometry;
  131. var bounds = geo.getBounds();
  132. var center = bounds.getCenterLonLat();
  133. var contentHTML = "<div style='font-size:.8em; opacity: 0.8; overflow-y:hidden;'>";
  134. contentHTML += "<div>" + feature.info.name + "</div></div>";
  135. var popup = new SuperMap.Popup.FramedCloud("popwin",
  136. new SuperMap.LonLat(center.lon, center.lat),
  137. null,
  138. contentHTML,
  139. null,
  140. true);
  141. feature.popup = popup;
  142. infowin = popup;
  143. map.addPopup(popup);
  144. }
  145. function closeInfoWin() {
  146. if (infowin) {
  147. try {
  148. infowin.hide();
  149. infowin.destroy();
  150. }
  151. catch (e) {
  152. }
  153. }
  154. }
  155. </script>
  156. </body>
  157. </html>