f80_draw.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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>图上标绘 </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,terraformer,turf,mars3d,mars3d-widget"
  27. ></script>
  28. <!-- KML/KMZ 转 GeoJSON 格式 -->
  29. <script type="text/javascript" src="../lib/kml/kml-geojson.js"></script>
  30. <link href="css/style.css" rel="stylesheet" />
  31. <style>
  32. .infoview div {
  33. margin-bottom: 5px;
  34. }
  35. </style>
  36. </head>
  37. <body class="dark">
  38. <!--加载前进行操作提示,优化用户体验-->
  39. <div id="mask" class="signmask" onclick="removeMask()"></div>
  40. <div id="mars3dContainer" class="mars3d-container"></div>
  41. <div class="infoview">
  42. <div>
  43. 图层状态:
  44. <div class="checkbox checkbox-primary checkbox-inline">
  45. <input id="chkShow" class="styled" type="checkbox" checked />
  46. <label for="chkShow"> 显示隐藏 </label>
  47. </div>
  48. <div class="checkbox checkbox-primary checkbox-inline">
  49. <input id="chkPopup" class="styled" type="checkbox" />
  50. <label for="chkPopup"> Popup绑定 </label>
  51. </div>
  52. <div class="checkbox checkbox-primary checkbox-inline">
  53. <input id="chkTooltip" class="styled" type="checkbox" />
  54. <label for="chkTooltip"> Tooltip绑定 </label>
  55. </div>
  56. <div class="checkbox checkbox-primary checkbox-inline">
  57. <input id="chkContextMenu" class="styled" type="checkbox" checked />
  58. <label for="chkContextMenu"> 右键菜单绑定 </label>
  59. </div>
  60. <div class="checkbox checkbox-primary checkbox-inline">
  61. <input id="chkHasEdit" class="styled" type="checkbox" checked />
  62. <label for="chkHasEdit"> 是否编辑 </label>
  63. </div>
  64. <div class="checkbox checkbox-primary checkbox-inline" title="屏蔽拾取地形坐标,避免穿透3dtiles模型">
  65. <input id="chk_onlyPickModelPosition" class="styled" type="checkbox" />
  66. <label for="chk_onlyPickModelPosition"> 仅在3dtiles上标绘 </label>
  67. </div>
  68. </div>
  69. <div>
  70. 图层管理:
  71. <input id="btnClear" type="button" class="btn btn-danger" value="清除" />
  72. <input id="btnImpFile" type="button" class="btn btn-primary" value="打开..." />
  73. <input id="btnExpFile" type="button" class="btn btn-warning" value="保存GeoJSON" title="保存为GeoJSON文件" />
  74. <input id="btnSaveKML" type="button" class="btn btn-warning" value="另存KML" />
  75. <input id="btnSaveWKT" type="button" class="btn btn-warning" value="另存WKT" />
  76. <input id="input_draw_file" type="file" accept=".json,.geojson,.kml,.kmz" style="display: none" />
  77. </div>
  78. <div>
  79. 单个点类:
  80. <input type="button" class="btn btn-primary" value="点" onclick="drawPoint()" />
  81. <input type="button" class="btn btn-primary" value="图标点" onclick="drawMarker()" />
  82. <input type="button" class="btn btn-primary" value="文字" onclick="drawLabel()" />
  83. <input type="button" class="btn btn-primary" value="小模型" onclick="drawModel()" />
  84. </div>
  85. <div>
  86. 二维空间:
  87. <input type="button" class="btn btn-primary" value="线" onclick="drawPolyline(false)" />
  88. <input type="button" class="btn btn-primary" value="曲线" onclick="drawCurve(false)" />
  89. <input type="button" class="btn btn-primary" value="走廊" onclick="drawCorridor(false)" />
  90. <input type="button" class="btn btn-primary" value="面" onclick="drawPolygon(false)" />
  91. <input type="button" class="btn btn-primary" value="圆" onclick="drawEllipse(false)" />
  92. <input type="button" class="btn btn-primary" value="矩形" onclick="drawRectangle(false)" />
  93. <input type="button" class="btn btn-primary" value="平面" onclick="draPlane()" />
  94. </div>
  95. <div>
  96. 二维贴地:
  97. <input type="button" class="btn btn-primary" value="线" onclick="drawPolyline(true)" />
  98. <input type="button" class="btn btn-primary" value="曲线" onclick="drawCurve(true)" />
  99. <input type="button" class="btn btn-primary" value="走廊" onclick="drawCorridor(true)" />
  100. <input type="button" class="btn btn-primary" value="面" onclick="drawPolygon(true)" />
  101. <input type="button" class="btn btn-primary" value="圆" onclick="drawEllipse(true)" />
  102. <input type="button" class="btn btn-primary" value="矩形" onclick="drawRectangle(true)" />
  103. </div>
  104. <div>
  105. 三维空间:
  106. <input type="button" class="btn btn-primary" value="墙" onclick="draWall(false)" />
  107. <input type="button" class="btn btn-primary" value="闭合墙" onclick="draWall(true)" />
  108. <input type="button" class="btn btn-primary" value="面立体" onclick="drawExtrudedPolygon()" />
  109. <input type="button" class="btn btn-primary" value="矩形立体" onclick="drawExtrudedRectangle()" />
  110. <input type="button" class="btn btn-primary" value="盒子" onclick="drawBox()" />
  111. <input type="button" class="btn btn-primary" value="圆柱" onclick="drawExtrudedCircle()" />
  112. <input type="button" class="btn btn-primary" value="圆锥" onclick="drawCylinder()" />
  113. <input type="button" class="btn btn-primary" value="球" onclick="drawEllipsoid()" />
  114. </div>
  115. </div>
  116. <!-- 切换视角到模型或地形(山区)的快捷按钮 -->
  117. <script type="text/javascript" src="js/center_terrain_3dtiles.js"></script>
  118. <!-- 图层管理相关处理js -->
  119. <script type="text/javascript" src="./js/graphicManager.js"></script>
  120. <script src="./js/common.js"></script>
  121. <script type="text/javascript">
  122. "use script"; //开发环境建议开启严格模式
  123. var map;
  124. var graphicLayer;
  125. function initMap(options) {
  126. //合并属性参数,可覆盖config.json中的对应配置
  127. var mapOptions = mars3d.Util.merge(options, {
  128. control: {
  129. infoBox: false,
  130. },
  131. });
  132. //创建三维地球场景
  133. map = new mars3d.Map("mars3dContainer", mapOptions);
  134. graphicLayer = new mars3d.layer.GraphicLayer({
  135. hasEdit: true,
  136. isAutoEditing: true, //绘制完成后是否自动激活编辑
  137. });
  138. map.addLayer(graphicLayer);
  139. //自定义提示
  140. // mars3d.Lang["_单击开始绘制"][0] = "新的提示内容";
  141. // mars3d.Lang["_单击增加点右击删除点"][0] = "新的提示内容";
  142. // mars3d.Lang["_双击完成绘制"][0] = "";
  143. //图层管理的相关处理,代码在\js\graphicManager.js
  144. window.initLayerManager(graphicLayer);
  145. graphicLayer.unbindPopup();
  146. //绑定标绘相关事件监听(可以自行加相关代码实现业务需求,此处主要做示例)
  147. graphicLayer.on(mars3d.EventType.drawStart, function (e) {
  148. console.log("开始绘制", e);
  149. });
  150. graphicLayer.on(mars3d.EventType.drawAddPoint, function (e) {
  151. console.log("绘制过程中增加了点", e);
  152. });
  153. graphicLayer.on(mars3d.EventType.drawRemovePoint, function (e) {
  154. console.log("绘制过程中删除了点", e);
  155. });
  156. graphicLayer.on(mars3d.EventType.drawCreated, function (e) {
  157. console.log("创建完成", e);
  158. });
  159. graphicLayer.on(mars3d.EventType.editStart, function (e) {
  160. console.log("开始编辑", e);
  161. startEditing(e.graphic);
  162. });
  163. graphicLayer.on(mars3d.EventType.editMovePoint, function (e) {
  164. console.log("编辑修改了点", e);
  165. startEditing(e.graphic);
  166. });
  167. graphicLayer.on(mars3d.EventType.editRemovePoint, function (e) {
  168. console.log("编辑删除了点", e);
  169. startEditing(e.graphic);
  170. });
  171. graphicLayer.on(mars3d.EventType.editStop, function (e) {
  172. console.log("停止编辑", e);
  173. stopEditing();
  174. });
  175. graphicLayer.on(mars3d.EventType.removeGraphic, function (e) {
  176. console.log("删除了对象", e);
  177. stopEditing();
  178. });
  179. $("#chk_onlyPickModelPosition").change(function () {
  180. var val = $(this).is(":checked");
  181. //控制鼠标只取模型上的点,忽略地形上的点的拾取
  182. map.onlyPickModelPosition = val;
  183. });
  184. $("#btnSaveKML").click(function () {
  185. if (graphicLayer.length === 0) {
  186. window.layer.msg("当前没有标注任何数据,无需保存!");
  187. return;
  188. }
  189. var strResult = toKML();
  190. haoutil.file.downloadFile("我的标注.kml", strResult);
  191. });
  192. $("#btnSaveWKT").click(function () {
  193. if (graphicLayer.length === 0) {
  194. window.layer.msg("当前没有标注任何数据,无需保存!");
  195. return;
  196. }
  197. var strResult = toWKT();
  198. haoutil.file.downloadFile("我的标注wkt.txt", JSON.stringify(strResult));
  199. });
  200. if (location.hostname !== "127.0.0.1" && location.hostname !== "localhost") {
  201. queryDrawData().then(function (data) {
  202. graphicLayer.loadGeoJSON(data, { clear: true, flyTo: true });
  203. });
  204. }
  205. }
  206. //获取数据
  207. function queryDrawData() {
  208. //加载历史演示数据
  209. return new Promise(function (resolve, reject) {
  210. $.ajax({
  211. url: "//data.mars3d.cn/file/geojson/mars3d-draw.json",
  212. type: "get",
  213. dataType: "json",
  214. success: function (result) {
  215. resolve(result);
  216. },
  217. error: function (data) {
  218. reject(data);
  219. },
  220. });
  221. });
  222. }
  223. function drawPoint() {
  224. graphicLayer.startDraw({
  225. type: "point",
  226. style: {
  227. pixelSize: 12,
  228. color: "#3388ff",
  229. label: {
  230. //不需要文字时,去掉label配置即可
  231. text: "可以同时支持文字",
  232. font_size: 20,
  233. color: "#ffffff",
  234. outline: true,
  235. outlineColor: "#000000",
  236. pixelOffsetY: -20,
  237. },
  238. },
  239. });
  240. }
  241. function drawMarker() {
  242. graphicLayer.startDraw({
  243. type: "billboard",
  244. style: {
  245. image: "img/marker/mark1.png",
  246. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  247. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  248. label: {
  249. //不需要文字时,去掉label配置即可
  250. text: "可以同时支持文字",
  251. font_size: 26,
  252. color: "#ffffff",
  253. outline: true,
  254. outlineColor: "#000000",
  255. pixelOffsetY: -60,
  256. },
  257. },
  258. });
  259. }
  260. function drawLabel() {
  261. graphicLayer.startDraw({
  262. type: "label",
  263. style: {
  264. text: "火星科技三维地球",
  265. color: "#0081c2",
  266. font_size: 50,
  267. outline: true,
  268. outlineColor: "#ffffff",
  269. outlineWidth: 2,
  270. },
  271. });
  272. }
  273. function drawModel() {
  274. graphicLayer.startDraw({
  275. type: "model",
  276. style: {
  277. scale: 10,
  278. url: "//data.mars3d.cn/gltf/mars/firedrill/xiaofangche.gltf",
  279. },
  280. });
  281. }
  282. function drawPolyline(clampToGround) {
  283. graphicLayer.startDraw({
  284. type: "polyline",
  285. // maxPointNum: 2, //限定最大点数,可以绘制2个点的线,自动结束
  286. style: {
  287. color: clampToGround ? "#ffff00" : "#3388ff",
  288. width: 3,
  289. clampToGround: clampToGround,
  290. },
  291. });
  292. }
  293. function drawPolygon(clampToGround) {
  294. graphicLayer.startDraw({
  295. type: "polygon",
  296. style: {
  297. color: clampToGround ? "#ffff00" : "#3388ff",
  298. opacity: 0.5,
  299. outline: true,
  300. outlineColor: "#ffffff",
  301. outlineWidth: 2.0,
  302. clampToGround: clampToGround,
  303. },
  304. });
  305. }
  306. function drawCurve(clampToGround) {
  307. graphicLayer.startDraw({
  308. type: "curve",
  309. style: {
  310. color: clampToGround ? "#ffff00" : "#3388ff",
  311. width: 3,
  312. clampToGround: clampToGround,
  313. },
  314. });
  315. }
  316. function drawCorridor(clampToGround) {
  317. graphicLayer.startDraw({
  318. type: "corridor",
  319. style: {
  320. color: clampToGround ? "#ffff00" : "#3388ff",
  321. opacity: 0.6,
  322. width: 500,
  323. clampToGround: clampToGround,
  324. },
  325. });
  326. }
  327. function drawEllipse(clampToGround) {
  328. graphicLayer.startDraw({
  329. type: "circle",
  330. style: {
  331. color: clampToGround ? "#ffff00" : "#3388ff",
  332. opacity: 0.6,
  333. outline: true,
  334. outlineColor: "#ffffff",
  335. outlineWidth: 2.0,
  336. clampToGround: clampToGround,
  337. },
  338. });
  339. }
  340. function drawRectangle(clampToGround) {
  341. graphicLayer.startDraw({
  342. type: "rectangle",
  343. style: {
  344. color: clampToGround ? "#ffff00" : "#3388ff",
  345. opacity: 0.6,
  346. outline: true,
  347. outlineColor: "#ffffff",
  348. outlineWidth: 2.0,
  349. clampToGround: clampToGround,
  350. },
  351. });
  352. }
  353. function draPlane() {
  354. graphicLayer.startDraw({
  355. type: "plane",
  356. style: {
  357. color: "#00ff00",
  358. opacity: 0.8,
  359. plane_normal: "x",
  360. dimensions_x: 1000.0,
  361. dimensions_y: 1000.0,
  362. },
  363. });
  364. }
  365. function draWall(closure) {
  366. graphicLayer.startDraw({
  367. type: "wall",
  368. style: {
  369. color: "#00ff00",
  370. opacity: 0.8,
  371. diffHeight: 400,
  372. closure: closure, //是否闭合
  373. },
  374. });
  375. }
  376. function drawBox() {
  377. graphicLayer.startDraw({
  378. type: "box",
  379. style: {
  380. color: "#00ff00",
  381. opacity: 0.6,
  382. dimensions_x: 1000.0,
  383. dimensions_y: 1000.0,
  384. dimensions_z: 1000.0,
  385. },
  386. });
  387. }
  388. function drawCylinder() {
  389. graphicLayer.startDraw({
  390. type: "cylinder",
  391. style: {
  392. fill: true,
  393. color: "#00ff00",
  394. opacity: 0.6,
  395. length: 1000,
  396. },
  397. });
  398. }
  399. function drawEllipsoid() {
  400. graphicLayer.startDraw({
  401. type: "ellipsoid",
  402. style: {
  403. fill: true,
  404. color: "#00ff00",
  405. opacity: 0.6,
  406. },
  407. });
  408. }
  409. function drawExtrudedPolygon() {
  410. graphicLayer.startDraw({
  411. type: "polygon",
  412. style: {
  413. color: "#00ff00",
  414. opacity: 0.6,
  415. diffHeight: 300,
  416. },
  417. });
  418. }
  419. function drawExtrudedRectangle() {
  420. graphicLayer.startDraw({
  421. type: "rectangle",
  422. style: {
  423. color: "#00ff00",
  424. opacity: 0.6,
  425. diffHeight: 300,
  426. },
  427. });
  428. }
  429. function drawExtrudedCircle() {
  430. graphicLayer.startDraw({
  431. type: "circle",
  432. style: {
  433. color: "#00ff00",
  434. opacity: 0.6,
  435. diffHeight: 300,
  436. },
  437. });
  438. }
  439. function toKML() {
  440. var geojsonObject = graphicLayer.toGeoJSON();
  441. if (geojsonObject == null) {
  442. return null;
  443. }
  444. geojsonObject = haoutil.system.clone(geojsonObject);
  445. var kml = kgUtil.toKml(geojsonObject, {
  446. name: "Mars3D标绘数据",
  447. documentName: "Mars3D标绘数据文件",
  448. documentDescription: "标绘数据 by mars3d.cn",
  449. simplestyle: true,
  450. });
  451. return kml;
  452. }
  453. function toWKT() {
  454. var geojsonObject = graphicLayer.toGeoJSON();
  455. if (geojsonObject == null) {
  456. return null;
  457. }
  458. geojsonObject = haoutil.system.clone(geojsonObject);
  459. var arrWKT = [];
  460. var index = 0;
  461. geojsonObject.features.forEach((feature) => {
  462. var attr = feature.properties;
  463. var style = feature.properties.style;
  464. var wkt = Terraformer.WKT.convert(feature.geometry); //geojson转换WKT格式 ,terraformer库
  465. arrWKT.push({
  466. id: ++index,
  467. name: attr.name || "",
  468. remark: attr.remark || "",
  469. style: style,
  470. wkt: wkt,
  471. });
  472. });
  473. return arrWKT;
  474. }
  475. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  476. var timeTik;
  477. function startEditing(graphic) {
  478. clearTimeout(timeTik);
  479. var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
  480. if (plotAttr && plotAttr.isActivate) {
  481. plotAttr.startEditing(graphic, graphic.coordinates);
  482. } else {
  483. mars3d.widget.activate({
  484. map: map,
  485. uri: "widgets/plotAttr/widget.js",
  486. name: "属性编辑",
  487. graphic: graphic,
  488. lonlats: graphic.coordinates,
  489. });
  490. }
  491. }
  492. function stopEditing() {
  493. timeTik = setTimeout(function () {
  494. mars3d.widget.disable("widgets/plotAttr/widget.js");
  495. }, 200);
  496. }
  497. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  498. </script>
  499. </body>
  500. </html>