others_SM_OSMBuildings_DrawBuildings.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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>Supermap OSM BUildings Draw Data</title>
  9. <style type="text/css">
  10. .editPane {
  11. position: absolute;
  12. right: 10px;
  13. top: 50px;
  14. width: 350px;
  15. text-align: center;
  16. background: #FFF;
  17. z-index: 9999999;
  18. }
  19. </style>
  20. </head>
  21. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  22. <div class='panel panel-primary editPane' id='editPane'>
  23. <div class='panel-heading'>
  24. <h5 class='panel-title text-center'>Supermap OSM BUildings Draw Data</h5>
  25. </div>
  26. <div class='panel-body' id='params'>
  27. <div class='input-group'>
  28. <span class='input-group-addon' data-i18n="resources.text_topHeight"></span>
  29. <input id='building_maxheight' type='text' class='form-control' value="50"/>
  30. </div>
  31. <p></p>
  32. <div class='input-group'>
  33. <span class='input-group-addon' data-i18n="resources.text_bottomHeight"></span>
  34. <input id='building_minheight' type='text' class='form-control' value="0"/>
  35. </div>
  36. <p></p>
  37. <div class='input-group'>
  38. <span class='input-group-addon' data-i18n="resources.text_wallColor"></span>
  39. <input id='building_wallcolor' type='text' class='form-control' value="DDDDDD"/>
  40. </div>
  41. <p></p>
  42. <div class='input-group'>
  43. <span class='input-group-addon' data-i18n="resources.text_topColor"></span>
  44. <input id='building_roofcolor' type='text' class='form-control' value="F9F7F4"/>
  45. </div>
  46. </div>
  47. </div>
  48. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  49. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  50. <script type="text/javascript" exclude="iclient-classic" include="OSMBuildings-SuperMap"
  51. src="../../dist/classic/include-classic.js"></script>
  52. <script type="text/javascript">
  53. var map, layer, polygonLayer, drawPolygon, osm, featureID = 0,
  54. host = window.isLocal ? window.server : "https://iserver.supermap.io",
  55. url = host + "/iserver/services/map-china400/rest/maps/China";
  56. //检测浏览器是否支持canves,Buildings 需要Canvas绘制
  57. if (!document.createElement('canvas').getContext) {
  58. widgets.alert.showAlert(resources.msg_supportCanvas,false);
  59. }
  60. $(document).ready(function () {
  61. widgets.alert.showAlert(resources.msg_drawShape, false);
  62. });
  63. //新建矢量面图层 保存绘制的要素
  64. polygonLayer = new SuperMap.Layer.Vector("polygonLayer");
  65. drawPolygon = new SuperMap.Control.DrawFeature(polygonLayer, SuperMap.Handler.Polygon);
  66. //设置featureadded监听事件
  67. drawPolygon.events.on({"featureadded": drawCompleted});
  68. //初始化地图
  69. map = new SuperMap.Map("map", {
  70. controls: [
  71. new SuperMap.Control.Navigation(),
  72. new SuperMap.Control.MousePosition(),
  73. new SuperMap.Control.LayerSwitcher(),
  74. new SuperMap.Control.Zoom(),
  75. drawPolygon
  76. ]
  77. });
  78. //初始化图层
  79. layer = new SuperMap.Layer.TiledDynamicRESTLayer("China", url, null, {maxResolution: "auto"});
  80. //监听图层信息加载完成事件
  81. layer.events.on({"layerInitialized": addLayer});
  82. //异步加载图层
  83. function addLayer() {
  84. map.addLayers([layer, polygonLayer]);
  85. map.setCenter(new SuperMap.LonLat(12957186.36784, 4852711.53595), 12);
  86. //激活多边形绘制
  87. drawPolygon.activate();
  88. }
  89. //Buildings data 初始状态
  90. var Draw_geojson = {
  91. "type": "FeatureCollection",
  92. "features": []
  93. };
  94. //绘制完毕
  95. function drawCompleted(e) {
  96. featureID++;
  97. var feature = {
  98. "type": "Feature",
  99. "id": 0,
  100. "geometry": {
  101. "type": "Polygon",
  102. "coordinates": []
  103. },
  104. "properties": {}
  105. };
  106. //feature屬性设置
  107. feature.id = featureID;
  108. feature.properties.height = parseInt(document.getElementById("building_maxheight").value);
  109. feature.properties.minHeight = parseInt(document.getElementById("building_minheight").value);
  110. feature.properties.wallColor = document.getElementById("building_wallcolor").value;
  111. feature.properties.roofColor = document.getElementById("building_roofcolor").value;
  112. //提取e对象中的经纬度 生成feature 保存在Draw_geojson的features中
  113. var components1 = e.feature.geometry.components;
  114. var geometry_coordinates = [];
  115. for (var i = 0; i < components1.length; i++) {
  116. var components2 = components1[i];
  117. for (var j = 0; j < components2.components.length; j++) {
  118. var component = components2.components[j];
  119. var lonlat1 = new SuperMap.LonLat(component.x, component.y);
  120. //坐标系转换 OSM Buildings 数据坐标必须是 EPSG:4326
  121. var lonlat2 = lonlat1.transform(new SuperMap.Projection("EPSG:3857"), new SuperMap.Projection("EPSG:4326"));
  122. geometry_coordinates.push([lonlat2.lon, lonlat2.lat]);
  123. }
  124. //起点终点相同 闭合
  125. geometry_coordinates.push(geometry_coordinates[0]);
  126. feature.geometry.coordinates = [geometry_coordinates];
  127. Draw_geojson.features.push(feature);
  128. //判断是否已经创建了OSM Buildings图层
  129. if (map.getLayersByName("OSM Buildings").length) {
  130. osm.set(Draw_geojson);
  131. }
  132. else {
  133. osm = new OSMBuildings(map);
  134. osm.load();
  135. osm.date(new Date()); //设置当前时间 计算OSMBuildings 阴影
  136. osm.set(Draw_geojson);
  137. osm.click(function (e) {
  138. //Buildings 单击事件 返回当前Building的ID 和 经纬度
  139. widgets.alert.showAlert("FeatureID:" + e.feature,true);
  140. });
  141. }
  142. }
  143. }
  144. </script>
  145. </body>
  146. </html>