g15_dth_fd_edit.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>矢量单体化编辑(GeoJson叠加) </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,web-icons,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d,mars3d-widget"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. <style>
  30. .infoview div {
  31. margin-bottom: 5px;
  32. }
  33. </style>
  34. </head>
  35. <body class="dark">
  36. <!--加载前进行操作提示,优化用户体验-->
  37. <div id="mask" class="signmask" onclick="removeMask()"></div>
  38. <div id="mars3dContainer" class="mars3d-container"></div>
  39. <div class="infoview">
  40. <div>
  41. <div class="radio radio-info radio-inline">
  42. <input type="radio" id="queryContent1" name="queryContent" value="1" checked />
  43. <label for="queryContent1">编辑模式</label>
  44. </div>
  45. <div class="radio radio-info radio-inline">
  46. <input type="radio" id="queryContent2" name="queryContent" value="2" />
  47. <label for="queryContent2">预览模式</label>
  48. </div>
  49. </div>
  50. <div id="editView">
  51. <input type="button" class="btn btn-primary" value="单体化面" onclick="drawPolygon()" />
  52. <input type="button" class="btn btn-danger" value="清除" onclick="deleteAll()" />
  53. <input id="btnSave" type="button" class="btn btn-primary" value="保存" />
  54. <input id="btnImpFile" type="button" class="btn btn-primary" value="打开" />
  55. <input id="input_plot_file" type="file" accept=".json,.geojson" style="display: none" />
  56. </div>
  57. </div>
  58. <!-- 箭头等标号 外部扩展 -->
  59. <script src="./js/common.js"></script>
  60. <script type="text/javascript">
  61. "use script"; //开发环境建议开启严格模式
  62. var map;
  63. var graphicLayerEdit;
  64. var geoJsonLayerDTH;
  65. function initMap(options) {
  66. //合并属性参数,可覆盖config.json中的对应配置
  67. var mapOptions = mars3d.Util.merge(options, {
  68. scene: {
  69. center: { lat: 43.820826, lng: 125.144526, alt: 679, heading: 333, pitch: -32 },
  70. },
  71. control: {
  72. infoBox: false,
  73. },
  74. });
  75. //创建三维地球场景
  76. map = new mars3d.Map("mars3dContainer", mapOptions);
  77. //三维模型
  78. var tilesetLayer = new mars3d.layer.TilesetLayer({
  79. type: "3dtiles",
  80. name: "校园",
  81. url: "//data.mars3d.cn/3dtiles/qx-xuexiao/tileset.json",
  82. position: { alt: 15.8 },
  83. maximumScreenSpaceError: 1,
  84. maximumMemoryUsage: 1024,
  85. });
  86. map.addLayer(tilesetLayer);
  87. //单体化图层
  88. geoJsonLayerDTH = new mars3d.layer.GeoJsonLayer({
  89. name: "校园-单体化",
  90. symbol: {
  91. type: "polygonP",
  92. styleOptions: {
  93. //单体化默认显示样式
  94. color: "rgba(255, 255, 255, 0.01)",
  95. clampToGround: true,
  96. classification: true,
  97. buffer: 1,
  98. //单体化鼠标移入或单击后高亮的样式
  99. highlight: {
  100. type: mars3d.EventType.click,
  101. color: "rgba(255,255,0,0.4)",
  102. },
  103. },
  104. },
  105. popup: [
  106. { field: "name", name: "学校场所" },
  107. { field: "sfkf", name: "是否开放" },
  108. { field: "remark", name: "备注信息" },
  109. ],
  110. });
  111. map.addLayer(geoJsonLayerDTH);
  112. graphicLayerEdit = new mars3d.layer.GraphicLayer({
  113. hasEdit: true,
  114. isAutoEditing: true, //绘制完成后是否自动激活编辑
  115. symbol: {
  116. type: "polygon",
  117. styleOptions: {
  118. color: "rgba(255, 255, 0, 0.4)",
  119. clampToGround: true,
  120. },
  121. },
  122. });
  123. map.addLayer(graphicLayerEdit);
  124. //绑定事件
  125. graphicLayerEdit.on(mars3d.EventType.drawCreated, function (e) {
  126. //创建完成
  127. startEditing(e.graphic);
  128. });
  129. graphicLayerEdit.on(mars3d.EventType.editStart, function (e) {
  130. startEditing(e.graphic);
  131. });
  132. graphicLayerEdit.on(mars3d.EventType.editMovePoint, function (e) {
  133. startEditing(e.graphic);
  134. });
  135. graphicLayerEdit.on(mars3d.EventType.editRemovePoint, function (e) {
  136. startEditing(e.graphic);
  137. });
  138. graphicLayerEdit.on(mars3d.EventType.editStop, function (e) {
  139. //停止编辑
  140. stopEditing();
  141. saveEntity(e.graphic);
  142. });
  143. graphicLayerEdit.on(mars3d.EventType.removeGraphic, function (e) {
  144. deleteEntity(e.graphic);
  145. });
  146. $("#btnSave").click(function () {
  147. var strResult = graphicLayerEdit.toGeoJSON();
  148. if (strResult == null) {
  149. layer.msg("当前没有标注任何数据,无需保存!");
  150. return;
  151. }
  152. haoutil.file.downloadFile("单体化.json", JSON.stringify(strResult));
  153. });
  154. $("#input_plot_file").change(function (e) {
  155. var file = this.files[0];
  156. var fileName = file.name;
  157. var fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
  158. if (fileType != "json") {
  159. layer.msg("文件类型不合法,请选择json格式标注文件!");
  160. clearSelectFile();
  161. return;
  162. }
  163. if (window.FileReader) {
  164. var reader = new FileReader();
  165. reader.readAsText(file, "UTF-8");
  166. reader.onloadend = function (e) {
  167. var json = this.result;
  168. graphicLayerEdit.loadGeoJSON(json, {
  169. clear: true,
  170. flyTo: true,
  171. });
  172. clearSelectFile();
  173. };
  174. }
  175. });
  176. $("#btnImpFile").click(function () {
  177. $("#input_plot_file").click();
  178. });
  179. $('input:radio[name="queryContent"]').change(function () {
  180. var selectType = $(this).val();
  181. if (selectType == "1") {
  182. $("#editView").show();
  183. toBJMS();
  184. } else {
  185. $("#editView").hide();
  186. toYLMS();
  187. }
  188. });
  189. queryDthData().then(function (result) {
  190. graphicLayerEdit.loadGeoJSON(result);
  191. });
  192. }
  193. //加载数据
  194. function queryDthData() {
  195. return new Promise(function (resolve, reject) {
  196. $.ajax({
  197. url: "//data.mars3d.cn/file/geojson/dth-xuexiao-fd.json",
  198. type: "get",
  199. dataType: "json",
  200. success: function (result) {
  201. resolve(result);
  202. },
  203. error: function (data) {
  204. reject(data);
  205. },
  206. });
  207. });
  208. }
  209. //保存方法,可以将数据提交到后台
  210. function saveEntity(graphic) {
  211. var geojson = graphicLayerEdit.toGeoJSON(graphic); //将geojson存到数据库
  212. console.log("geojson", geojson);
  213. }
  214. //删除方法,可以将数据提交到后台
  215. function deleteEntity(graphic) {
  216. var entityId = graphic.id;
  217. }
  218. //切换到预览模式
  219. function toYLMS() {
  220. var geojson = graphicLayerEdit.toGeoJSON();
  221. geoJsonLayerDTH.load({ data: geojson });
  222. graphicLayerEdit.hasEdit = false;
  223. graphicLayerEdit.show = false;
  224. }
  225. //切换到编辑模式
  226. function toBJMS() {
  227. geoJsonLayerDTH.clear();
  228. graphicLayerEdit.hasEdit = true;
  229. graphicLayerEdit.show = true;
  230. }
  231. function clearSelectFile() {
  232. if (!window.addEventListener) {
  233. document.getElementById("input_plot_file").outerHTML += ""; //IE
  234. } else {
  235. document.getElementById("input_plot_file").value = ""; //FF
  236. }
  237. }
  238. function deleteAll() {
  239. graphicLayerEdit.clear();
  240. }
  241. function drawPolygon(clampToGround) {
  242. graphicLayerEdit.startDraw({
  243. type: "polygon",
  244. style: {
  245. color: "#ffff00",
  246. opacity: 0.3,
  247. clampToGround: true,
  248. },
  249. });
  250. }
  251. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  252. var timeTik;
  253. function startEditing(graphic) {
  254. clearTimeout(timeTik);
  255. var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
  256. if (plotAttr && plotAttr.isActivate) {
  257. plotAttr.startEditing(graphic, graphic.coordinates);
  258. } else {
  259. mars3d.widget.activate({
  260. map: map,
  261. uri: "widgets/plotAttr/widget.js",
  262. name: "属性编辑",
  263. graphic: graphic,
  264. lonlats: graphic.coordinates,
  265. attrList: [
  266. { name: "id", label: "主键", type: "hidden", defval: "" },
  267. { name: "name", label: "学校场所", type: "text", defval: "" },
  268. { name: "sfkf", label: "是否开放", type: "text", defval: "" },
  269. { name: "remark", label: "备注信息", type: "textarea", defval: "" },
  270. ],
  271. });
  272. }
  273. }
  274. function stopEditing() {
  275. timeTik = setTimeout(function () {
  276. mars3d.widget.disable("widgets/plotAttr/widget.js");
  277. }, 200);
  278. }
  279. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  280. </script>
  281. </body>
  282. </html>