theme_themeLabelLayer.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <title data-i18n="resources.title_themeLabelLayer"></title>
  9. <style type="text/css">
  10. .editPane {
  11. position: absolute;
  12. right: 50px;
  13. top: 50px;
  14. text-align: center;
  15. background: #FFF;
  16. z-index: 1000;
  17. }
  18. </style>
  19. </head>
  20. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  21. <div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
  22. <div class='panel-heading'>
  23. <h5 class='panel-title text-center' data-i18n="resources.title_themeLabelLayer"></h5>
  24. </div>
  25. <div class='panel-body' id='params'>
  26. <p></p>
  27. <div align='right' class='button-group'>
  28. <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_addLabel"
  29. onclick="addLabel()"/>
  30. <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear"
  31. onclick="clearLabels()"/>
  32. </div>
  33. </div>
  34. </div>
  35. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  36. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  37. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  38. <script type="text/javascript">
  39. var map, layer, vectorLayer, selectFeature, popup, strategy,
  40. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  41. url = host + "/iserver/services/map-world/rest/maps/World";
  42. map = new SuperMap.Map("map", {
  43. controls: [
  44. new SuperMap.Control.LayerSwitcher(),
  45. new SuperMap.Control.ScaleLine(),
  46. new SuperMap.Control.Zoom(),
  47. new SuperMap.Control.Navigation({
  48. dragPanOptions: {
  49. enableKinetic: true
  50. }
  51. })]
  52. });
  53. layer = new SuperMap.Layer.TiledDynamicRESTLayer("世界地图", url, {
  54. transparent: true,
  55. cacheEnabled: true
  56. }, {maxResolution: "auto"});
  57. layer.events.on({"layerInitialized": addLayer});
  58. //新建一个策略
  59. strategy = new SuperMap.Strategy.GeoText();
  60. //新建一个标签专题图层
  61. vectorLayer = new SuperMap.Layer.Vector("Label", {strategies: [strategy]});
  62. //设置标签的样式
  63. strategy.style = {
  64. fontColor: "#FF7F00",
  65. fontWeight: "bolder",
  66. fontSize: "14px",
  67. fill: true,
  68. fillColor: "#FFFFFF",
  69. fillOpacity: 1,
  70. stroke: true,
  71. strokeColor: "#8B7B8B"
  72. };
  73. //用于标签分组的属性字段名称
  74. strategy.groupField = "CAP_POP";
  75. //标签分组数组
  76. strategy.styleGroups = [
  77. {
  78. start: 200000,
  79. end: 500000,
  80. style: {
  81. fontColor: "#FF4500",
  82. fontWeight: "bolder",
  83. fontSize: "18px"
  84. }
  85. },
  86. {
  87. start: 500000,
  88. end: 1000000,
  89. style: {
  90. fontColor: "#FF0000",
  91. fontWeight: "bolder",
  92. fontSize: "22px"
  93. }
  94. },
  95. {
  96. start: 1000000,
  97. end: 2000000,
  98. style: {
  99. fontColor: "#EE0000",
  100. fontWeight: "bolder",
  101. fontSize: "26px"
  102. }
  103. },
  104. {
  105. start: 2000000,
  106. end: 100000000,
  107. style: {
  108. fontColor: "#CD0000",
  109. fontWeight: "bolder",
  110. fontSize: "30px"
  111. }
  112. }
  113. ];
  114. function addLayer() {
  115. map.addLayers([layer, vectorLayer]);
  116. map.setCenter(new SuperMap.LonLat(0, 0), 1);
  117. //通过selectFeature控件为标签添加点击事件
  118. var callbacks = {
  119. click: function (feature) {
  120. closeInfoWin();
  121. //组织弹窗内容
  122. var contentHTML = "<div style=\'font-size:.8em; opacity: 0.8; overflow-y:hidden;\'>";
  123. contentHTML += "<div>" + resources.text_inCountry + feature.attributes.COUNTRY + "</div>";
  124. contentHTML += "<div>ID:" + feature.attributes.SmID + "</div>";
  125. contentHTML += "<div>"+resources.text_countsPOP + feature.attributes.CAP_POP + "</div>" + "</div>";
  126. //新建一个弹窗并加入地图
  127. popup = new SuperMap.Popup.FramedCloud("popwin", new SuperMap.LonLat(feature.attributes.SmX, feature.attributes.SmY), null, contentHTML, null, true, function () {
  128. closeInfoWin();
  129. });
  130. map.addPopup(popup);
  131. }
  132. };
  133. //实例化 selectFeature 控件
  134. selectFeature = new SuperMap.Control.SelectFeature(vectorLayer, {callbacks: callbacks});
  135. //map上添加控件
  136. map.addControl(selectFeature);
  137. //激活控件
  138. selectFeature.activate();
  139. }
  140. //关闭弹窗
  141. function closeInfoWin() {
  142. if (popup) {
  143. try {
  144. map.removePopup(popup);
  145. }
  146. catch (e) {
  147. }
  148. }
  149. }
  150. //使用SQL查询Capitals数据集
  151. function addLabel() {
  152. clearLabels();
  153. var queryParam, queryBySQLParams, queryBySQLService;
  154. queryParam = new SuperMap.REST.FilterParameter({
  155. name: "Capitals@World.1"
  156. });
  157. queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
  158. queryParams: [queryParam]
  159. });
  160. queryBySQLService = new SuperMap.REST.QueryBySQLService(url, {
  161. eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}
  162. });
  163. queryBySQLService.processAsync(queryBySQLParams);
  164. }
  165. function processCompleted(queryEventArgs) {
  166. var i, j, labelFeas = [], //文本标签要素数组
  167. label, //文本标签要素
  168. feature,
  169. result = queryEventArgs.result;
  170. if (result && result.recordsets) {
  171. for (i = 0; i < result.recordsets.length; i++) {
  172. if (result.recordsets[i].features) {
  173. for (j = 0; j < result.recordsets[i].features.length; j++) {
  174. feature = result.recordsets[i].features[j];
  175. if (feature.attributes.CAPITAL) {
  176. //新建GeoText对象(文本标签)
  177. label = new SuperMap.Geometry.GeoText(feature.attributes.SmX, feature.attributes.SmY, feature.attributes.CAPITAL);
  178. //新建标签要素并添加到标签要素数组。注:标签要素是指 geometry 类型为 SuperMap.Geometry.GeoText 的矢量要素(SuperMap.Feature.Vector)。
  179. labelFeas.push(new SuperMap.Feature.Vector(label, feature.attributes));
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //将标签要素添加到标签专题图层中
  186. vectorLayer.addFeatures(labelFeas);
  187. }
  188. function processFailed(e) {
  189. widgets.alert.showAlert(e.error.errorMsg, false);
  190. }
  191. //清楚标签专题图层
  192. function clearLabels() {
  193. closeInfoWin();
  194. vectorLayer.removeAllFeatures();
  195. }
  196. </script>
  197. </body>
  198. </html>