vizLayer_cartoCSS_hightlight.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_cartoCSSHightlight"></title>
  9. <style type="text/css">
  10. body {
  11. margin: 0;
  12. overflow: hidden;
  13. background: #fff;
  14. width: 100%;
  15. height: 100%
  16. }
  17. #map {
  18. position: absolute;
  19. width: 100%;
  20. height: 100%;
  21. }
  22. #toolbar {
  23. position: absolute;
  24. top: 50px;
  25. right: 10px;
  26. text-align: center;
  27. z-index: 100;
  28. border-radius: 4px;
  29. }
  30. .layerItems {
  31. border: 1px solid #999;
  32. display: none;
  33. position: absolute;
  34. z-index: 1000;
  35. background-color: rgba(255,255,255,0.5);
  36. padding-right: 20px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="toolbar" class="panel panel-primary">
  42. <div class='panel-heading'>
  43. <h5 class='panel-title text-center' data-i18n="resources.text_cartoCSSHightlight"></h5></div>
  44. <div class='panel-body content'>
  45. <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_cancelHighlight"
  46. onclick="closeInfoWin();"/>
  47. </div>
  48. </div>
  49. <div class="layerItems">选择需要高亮的要素:
  50. <ul id='layerItems'></ul>
  51. </div>
  52. <div id="map"></div>
  53. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  54. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  55. <script type="text" id="textData">
  56. @color:#ee9900;
  57. *{
  58. line-color:@color;
  59. line-opacity:1;
  60. line-width:3;
  61. polygon-fill:@color;
  62. polygon-opacity:0.4;
  63. text-fill:#000000;
  64. }
  65. </script>
  66. <script>
  67. var map, layer, layerItems,layerItemsContainer, infowin, featureInfoes,
  68. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  69. url = host + "/iserver/services/map-china400/rest/maps/China";
  70. init();
  71. function init() {
  72. if (!document.createElement('canvas').getContext) {
  73. widgets.alert.showAlert(resources.msg_supportCanvas, false);
  74. return;
  75. }
  76. layerItemsContainer = document.getElementsByClassName('layerItems')[0];
  77. layerItems = document.getElementById('layerItems');
  78. map = new SuperMap.Map("map", {
  79. controls: [
  80. new SuperMap.Control.ScaleLine(),
  81. new SuperMap.Control.Zoom(),
  82. new SuperMap.Control.MousePosition(),
  83. new SuperMap.Control.Navigation({
  84. dragPanOptions: {
  85. enableKinetic: true
  86. }
  87. })]
  88. });
  89. var hightLightCartoCss = document.getElementById("textData").text;
  90. layer = new SuperMap.Layer.TiledVectorLayer("China", url, {
  91. cacheEnabled: true,
  92. returnAttributes: true
  93. }, {useLocalStorage: true, highLightCartoCss: hightLightCartoCss});
  94. layer.events.on({"layerInitialized": addLayer});
  95. map.events.on({
  96. 'click': function (evt) {
  97. closeInfoWin();
  98. layer.unHightlightFeatures();
  99. featureInfoes = layer.getFeature(evt.xy.x, evt.xy.y);
  100. console.log(featureInfoes);
  101. if (featureInfoes && featureInfoes.length > 0) {
  102. while (layerItems.firstChild) {
  103. layerItems.removeChild(layerItems.firstChild);
  104. }
  105. layerItemsContainer.style.top = evt.clientY + 'px';
  106. layerItemsContainer.style.left = evt.clientX + 'px';
  107. for (var i = 0, len = featureInfoes.length; i < len; i++) {
  108. var li = document.createElement('li');
  109. li.innerHTML = featureInfoes[i].cartoLayer.layerName;
  110. console.log(featureInfoes[i]);
  111. li.setAttribute('data-index', i);
  112. layerItems.appendChild(li);
  113. if (i !== (len - 1)) {
  114. li.style.borderBottom = '1px solid';
  115. }
  116. li.onclick = liClickHandle;
  117. }
  118. layerItemsContainer.style.display = 'block';
  119. } else {
  120. layerItemsContainer.style.display = 'none';
  121. }
  122. },
  123. 'rightclick': function () {
  124. layerItemsContainer.style.display = 'none';
  125. },
  126. 'move': function () {
  127. layerItemsContainer.style.display = 'none';
  128. }
  129. });
  130. }
  131. function liClickHandle(evt) {
  132. var evt = window.event || evt;
  133. var target = evt.srcElement || evt.target;
  134. evt.stopPropagation();
  135. var index = +target.dataset.index;
  136. layer.highlightFeatures(featureInfoes[index]);
  137. var lonlat = map.getLonLatFromViewPortPx(featureInfoes.xy);
  138. openPopup(featureInfoes[index].feature, lonlat);
  139. layerItemsContainer.style.display = 'none';
  140. }
  141. function addLayer() {
  142. map.addLayers([layer]);
  143. var center = new SuperMap.LonLat(0, 0);
  144. map.setCenter(center, 4);
  145. }
  146. //定义mouseClickHandler函数,触发click事件会调用此函数
  147. function openPopup(feature, lonlat) {
  148. var key = 'NAME';
  149. var val = feature && feature.attributes && feature.attributes[key];
  150. if (!val) {
  151. key = 'SmID';
  152. val = feature && feature.attributes && feature.attributes[key];
  153. }
  154. var contentHTML = "<div style='width:80px; font-size:12px;font-weight:bold ; opacity: 0.8'>";
  155. contentHTML += key + ":" + val;
  156. contentHTML += "</div>";
  157. //初始化FramedCloud类
  158. framedCloud = new SuperMap.Popup.FramedCloud(
  159. "chicken",
  160. lonlat,
  161. null,
  162. contentHTML,
  163. null,
  164. true,
  165. null,
  166. true
  167. );
  168. infowin = framedCloud;
  169. map.addPopup(framedCloud);
  170. }
  171. function closeInfoWin() {
  172. layer.unHightlightFeatures();
  173. if (infowin) {
  174. try {
  175. infowin.hide();
  176. infowin.destroy();
  177. }
  178. catch (e) {
  179. }
  180. }
  181. }
  182. </script>
  183. </body>
  184. </html>