g05_draw_gltf.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- 标题及搜索关键字 -->
  5. <link rel="shortcut icon" type="image/x-icon" href="" />
  6. <title>gltf模型标绘(含参数编辑) </title>
  7. <!--第三方lib-->
  8. <script
  9. type="text/javascript"
  10. src="../lib/include-lib.js"
  11. libpath="../lib/"
  12. include="jquery,font-awesome,web-icons,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d,mars3d-widget,localforage"
  13. ></script>
  14. <link href="css/style.css" rel="stylesheet" />
  15. <style>
  16. .infoview div {
  17. margin-bottom: 5px;
  18. }
  19. </style>
  20. </head>
  21. <body class="dark">
  22. <!--加载前进行操作提示,优化用户体验-->
  23. <div id="mask" class="signmask" onclick="removeMask()"></div>
  24. <div id="mars3dContainer" class="mars3d-container"></div>
  25. <div class="infoview">
  26. <div>
  27. 模型URL:
  28. <input id="txtModel" type="text" value="//data.mars3d.cn/gltf/mars/feiji.glb" class="form-control" style="width: 350px" />
  29. <div class="checkbox checkbox-primary checkbox-inline" title="解决跨域问题">
  30. <input id="chkProxy" class="styled" type="checkbox" />
  31. <label for="chkProxy"> 代理 </label>
  32. </div>
  33. <input type="button" class="btn btn-primary" title="单击后在地图上点选位置即可" value="标绘" onclick="drawModel()" />
  34. </div>
  35. <div>
  36. <div class="checkbox checkbox-primary checkbox-inline" style="margin-left: 10px">
  37. <input id="chkHasTerrain" class="styled" type="checkbox" checked />
  38. <label for="chkHasTerrain"> 地形 </label>
  39. </div>
  40. <div class="checkbox checkbox-primary checkbox-inline">
  41. <input id="chkTestTerrain" class="styled" type="checkbox" />
  42. <label for="chkTestTerrain"> 深度检测 </label>
  43. </div>
  44. <div class="checkbox checkbox-primary checkbox-inline" title="屏蔽拾取地形坐标,避免穿透3dtiles模型">
  45. <input id="chk_onlyPickModelPosition" class="styled" type="checkbox" />
  46. <label for="chk_onlyPickModelPosition"> 仅在3dtiles上标绘 </label>
  47. </div>
  48. <input id="btnImpFile" type="button" class="btn btn-primary" value="打开" />
  49. <input id="btnSave" type="button" class="btn btn-primary" value="保存" />
  50. <input id="input_plot_file" type="file" accept=".json,.geojson" style="display: none" />
  51. <input type="button" class="btn btn-danger" value="清除" onclick="deleteAll()" />
  52. </div>
  53. </div>
  54. <!-- 切换视角到模型或地形(山区)的快捷按钮 -->
  55. <script type="text/javascript" src="js/center_terrain_3dtiles.js"></script>
  56. <script src="./js/common.js"></script>
  57. <script type="text/javascript">
  58. "use script"; //开发环境建议开启严格模式
  59. var map;
  60. var graphicLayer;
  61. function initMap(options) {
  62. //合并属性参数,可覆盖config.json中的对应配置
  63. var mapOptions = mars3d.Util.merge(options, {
  64. scene: {
  65. center: { lat: 30.83351, lng: 116.354467, alt: 2743, heading: 359, pitch: -52 },
  66. clock: {
  67. currentTime: "2021-01-01 22:00:00",
  68. },
  69. },
  70. });
  71. //创建三维地球场景
  72. map = new mars3d.Map("mars3dContainer", mapOptions);
  73. //固定光照,避免gltf模型随时间存在亮度不一致。
  74. map.fixedLight = true;
  75. this.graphicLayer = new mars3d.layer.GraphicLayer({
  76. hasEdit: true,
  77. isAutoEditing: true, //绘制完成后是否自动激活编辑
  78. });
  79. map.addLayer(graphicLayer);
  80. var url = haoutil.system.getRequestByName("url");
  81. if (url) {
  82. //url传入模型地址
  83. $("#txtModel").val(url);
  84. console.log("a");
  85. } else {
  86. //显示历史数据
  87. localforage.getItem("g05_draw_gltf").then(function (value) {
  88. $("#txtModel").val(value);
  89. });
  90. }
  91. var globe = map.scene.globe;
  92. $("#chkHasTerrain").change(function () {
  93. var isStkTerrain = $(this).is(":checked");
  94. map.hasTerrain = isStkTerrain;
  95. });
  96. $("#chkTestTerrain").change(function () {
  97. var val = $(this).is(":checked");
  98. map.scene.globe.depthTestAgainstTerrain = val;
  99. if (val) {
  100. toastr.info("深度监测打开后,您将无法看到地下或被地形遮挡的对象。");
  101. }
  102. });
  103. $("#chk_onlyPickModelPosition").change(function () {
  104. var val = $(this).is(":checked");
  105. //控制鼠标只取模型上的点,忽略地形上的点的拾取
  106. map.onlyPickModelPosition = val;
  107. });
  108. //创建完成,事件监听
  109. graphicLayer.on(mars3d.EventType.drawCreated, function (e) {
  110. var graphic = e.graphic;
  111. if (!graphicLayer.isContinued) {
  112. startEditing(graphic);
  113. }
  114. });
  115. //编辑修改了模型
  116. graphicLayer.on(
  117. [mars3d.EventType.editStart, mars3d.EventType.editMovePoint, mars3d.EventType.editStyle, mars3d.EventType.editRemovePoint],
  118. function (e) {
  119. var graphic = e.graphic;
  120. startEditing(graphic);
  121. }
  122. );
  123. //停止编辑修改模型
  124. graphicLayer.on([mars3d.EventType.editStop, mars3d.EventType.removeGraphic], function (e) {
  125. stopEditing();
  126. });
  127. $("#btnSave").click(function () {
  128. var strResult = graphicLayer.toGeoJSON();
  129. if (strResult == null) {
  130. layer.msg("当前没有标注任何数据,无需保存!");
  131. return;
  132. }
  133. haoutil.file.downloadFile("模型标绘.json", JSON.stringify(strResult));
  134. });
  135. $("#input_plot_file").change(function (e) {
  136. var file = this.files[0];
  137. var fileName = file.name;
  138. var fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
  139. if (fileType != "json") {
  140. layer.msg("文件类型不合法,请选择json格式标注文件!");
  141. clearSelectFile();
  142. return;
  143. }
  144. if (window.FileReader) {
  145. var reader = new FileReader();
  146. reader.readAsText(file, "UTF-8");
  147. reader.onloadend = function (e) {
  148. var json = this.result;
  149. graphicLayer.loadGeoJSON(json, {
  150. clear: true,
  151. flyTo: true,
  152. });
  153. clearSelectFile();
  154. };
  155. }
  156. });
  157. $("#btnImpFile").click(function () {
  158. $("#input_plot_file").click();
  159. });
  160. }
  161. function clearSelectFile() {
  162. if (!window.addEventListener) {
  163. document.getElementById("input_plot_file").outerHTML += ""; //IE
  164. } else {
  165. document.getElementById("input_plot_file").value = ""; //FF
  166. }
  167. }
  168. function deleteAll() {
  169. graphicLayer.clear();
  170. }
  171. function drawModel() {
  172. var url = $("#txtModel").val();
  173. localforage.setItem("g05_draw_gltf", url);
  174. var isProxy = $("#chkProxy").is(":checked");
  175. if (isProxy) {
  176. url = "//server.mars3d.cn/proxy/" + url;
  177. }
  178. graphicLayer.startDraw({
  179. type: "model",
  180. drawShow: true, //绘制时,是否显示模型,可避免在3dtiles上拾取坐标存在问题。
  181. // editType: 'point', //编辑方式:只调整四周方向和比例
  182. style: {
  183. scale: 1,
  184. url: url,
  185. },
  186. });
  187. }
  188. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  189. var timeTik;
  190. function startEditing(graphic) {
  191. clearTimeout(timeTik);
  192. var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
  193. if (plotAttr && plotAttr.isActivate) {
  194. plotAttr.startEditing(graphic, graphic.coordinates);
  195. } else {
  196. mars3d.widget.activate({
  197. map: map,
  198. uri: "widgets/plotAttr/widget.js",
  199. name: "属性编辑",
  200. graphic: graphic,
  201. lonlats: graphic.coordinates,
  202. });
  203. }
  204. }
  205. function stopEditing() {
  206. timeTik = setTimeout(function () {
  207. mars3d.widget.disable("widgets/plotAttr/widget.js");
  208. }, 200);
  209. }
  210. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  211. </script>
  212. </body>
  213. </html>