query_queryByBounds.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_queryByBounds"></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. </style>
  31. </head>
  32. <body>
  33. <div id="toolbar" class="panel panel-primary">
  34. <div class='panel-heading'>
  35. <h5 class='panel-title text-center' data-i18n="resources.title_queryByBounds"></h5></div>
  36. <div class='panel-body content'>
  37. <input type="button" class="btn btn-default" data-i18n="[value]resources.text_query" onclick="drawGeometry()"/>&nbsp;
  38. <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearFeatures()"/>
  39. </div>
  40. </div>
  41. <div id="map"></div>
  42. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  43. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  44. <script>
  45. var map, local, layer, vectorLayer, control, queryBounds, markerLayer, drawFeature,
  46. style = {
  47. strokeColor: "#304DBE",
  48. strokeWidth: 1,
  49. pointerEvents: "visiblePainted",
  50. fillColor: "#304DBE",
  51. fillOpacity: 0.3
  52. },
  53. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  54. url = host + "/iserver/services/map-world/rest/maps/World";
  55. init();
  56. function init() {
  57. map = new SuperMap.Map("map", {
  58. controls: [
  59. new SuperMap.Control.ScaleLine(),
  60. new SuperMap.Control.Zoom(),
  61. new SuperMap.Control.Navigation({
  62. dragPanOptions: {
  63. enableKinetic: true
  64. }
  65. })]
  66. });
  67. map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
  68. layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
  69. transparent: true,
  70. cacheEnabled: true
  71. }, {maxResolution: "auto"});
  72. layer.events.on({"layerInitialized": addLayer});
  73. vectorLayer = new SuperMap.Layer.Vector("Vector Layer");//新建一个vectorLayer的矢量图层
  74. markerLayer = new SuperMap.Layer.Markers("Markers");//创建一个有标签的图层
  75. drawFeature = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Box, {"handlerOptions": {"cursorCSS": "crosshair"}});
  76. drawFeature.events.on({"featureadded": drawCompleted});
  77. map.addControl(drawFeature);
  78. }
  79. function addLayer() {
  80. map.addLayers([layer, vectorLayer, markerLayer]);
  81. map.setCenter(new SuperMap.LonLat(0, 0), 0);
  82. }
  83. function drawGeometry() {
  84. //先清除上次的显示结果
  85. clearFeatures();
  86. drawFeature.activate();
  87. }
  88. function drawCompleted(obj) {
  89. drawFeature.deactivate();
  90. var feature = obj.feature;
  91. feature.style = style;
  92. vectorLayer.addFeatures(feature);
  93. var queryBounds = feature.geometry.bounds;
  94. var queryParam, queryByBoundsParams, queryService;
  95. queryParam = new SuperMap.REST.FilterParameter({name: "Capitals@World.1"});//FilterParameter设置查询条件,name是必设的参数,(图层名称格式:数据集名称@数据源别名)
  96. queryByBoundsParams = new SuperMap.REST.QueryByBoundsParameters({
  97. queryParams: [queryParam],
  98. bounds: queryBounds
  99. });//queryParams查询过滤条件参数数组。bounds查询范围
  100. queryService = new SuperMap.REST.QueryByBoundsService(url, {
  101. eventListeners: {
  102. "processCompleted": processCompleted,
  103. "processFailed": processFailed
  104. }
  105. });
  106. queryService.processAsync(queryByBoundsParams);//向服务端传递参数,然后服务端返回对象
  107. }
  108. function processCompleted(queryEventArgs) {
  109. var i, j, result = queryEventArgs.result, marker;//queryEventArgs服务端返回的对象
  110. if (result && result.recordsets) {
  111. for (i = 0, recordsets = result.recordsets, len = recordsets.length; i < len; i++) {
  112. if (recordsets[i].features) {
  113. for (j = 0; j < recordsets[i].features.length; j++) {
  114. var f = recordsets[i].features[j];
  115. var point = f.geometry,
  116. size = new SuperMap.Size(44, 33),
  117. offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
  118. icon = new SuperMap.Icon("./images/marker.png", size, offset);
  119. marker = new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon);
  120. marker.sm_capital = f.attributes.CAPITAL;
  121. marker.events.on({
  122. "click": openInfoWin,
  123. "touchstart": openInfoWin, //假如要在移动端的浏览器也实现点击弹框,则在注册touch类事件
  124. "scope": marker
  125. });
  126. markerLayer.addMarker(marker);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. function processFailed(e) {
  133. widgets.alert.showAlert(e.error.errorMsg, false);
  134. }
  135. function clearFeatures() {
  136. vectorLayer.removeAllFeatures();
  137. markerLayer.clearMarkers();
  138. closeInfoWin();
  139. }
  140. var infowin = null;
  141. function openInfoWin() {
  142. closeInfoWin();
  143. var marker = this;
  144. var lonlat = marker.getLonLat();
  145. var contentHTML = "<div style='font-size:.8em; opacity: 0.8; overflow-y:hidden;'>";
  146. contentHTML += "<div>" + marker.sm_capital + "</div></div>";
  147. var size = new SuperMap.Size(0, 33);
  148. var offset = new SuperMap.Pixel(0, -size.h);
  149. var icon = new SuperMap.Icon("./images/marker.png", size, offset);
  150. var popup = new SuperMap.Popup.FramedCloud("popwin",
  151. new SuperMap.LonLat(lonlat.lon, lonlat.lat),
  152. null,
  153. contentHTML,
  154. icon,
  155. true);
  156. infowin = popup;
  157. map.addPopup(popup);
  158. }
  159. function closeInfoWin() {
  160. if (infowin) {
  161. try {
  162. infowin.hide();
  163. infowin.destroy();
  164. }
  165. catch (e) {
  166. }
  167. }
  168. }
  169. </script>
  170. </body>
  171. </html>