d15_contextmenu.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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,bootstrap,layer,haoutil,turf,mars3d"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. </head>
  30. <body class="dark">
  31. <!--加载前进行操作提示,优化用户体验-->
  32. <div id="mask" class="signmask" onclick="removeMask()"></div>
  33. <div id="mars3dContainer" class="mars3d-container"></div>
  34. <script src="./js/common.js"></script>
  35. <script type="text/javascript">
  36. "use script"; //开发环境建议开启严格模式
  37. var map;
  38. function initMap(options) {
  39. //合并属性参数,可覆盖config.json中的对应配置
  40. var mapOptions = mars3d.Util.merge(options, {});
  41. //创建三维地球场景
  42. map = new mars3d.Map("mars3dContainer", mapOptions);
  43. //演示3种不同层次的绑定方式,可以按需使用
  44. bindMapDemo();
  45. bindLayerDemo();
  46. bindGraphicDemo();
  47. }
  48. //1.在map地图上绑定右键菜单
  49. function bindMapDemo() {
  50. //内置的默认右键菜单获取方法
  51. // var defaultContextmenuItems =map.getDefaultContextMenu()
  52. //可以删减defaultContextmenuItems数组内值
  53. // map.bindContextMenu(defaultContextmenuItems)
  54. //解除已绑定的右键菜单
  55. map.unbindContextMenu();
  56. var mapContextmenuItems = [
  57. {
  58. text: "显示此处经纬度",
  59. iconCls: "fa fa-info-circle",
  60. show: function (e) {
  61. return Cesium.defined(e.cartesian);
  62. },
  63. callback: function (e) {
  64. var mpt = mars3d.LatLngPoint.fromCartesian(e.cartesian);
  65. haoutil.alert(mpt.toString(), "位置信息");
  66. },
  67. },
  68. {
  69. text: "查看当前视角",
  70. iconCls: "fa fa-camera-retro",
  71. callback: function (e) {
  72. let mpt = JSON.stringify(map.getCameraView());
  73. haoutil.alert(mpt, "当前视角信息");
  74. },
  75. },
  76. {
  77. text: "开启深度监测",
  78. iconCls: "fa fa-eye-slash",
  79. show: function () {
  80. return !map.scene.globe.depthTestAgainstTerrain;
  81. },
  82. callback: function (e) {
  83. map.scene.globe.depthTestAgainstTerrain = true;
  84. },
  85. },
  86. {
  87. text: "关闭深度监测",
  88. iconCls: "fa fa-eye",
  89. show: function () {
  90. return map.scene.globe.depthTestAgainstTerrain;
  91. },
  92. callback: function (e) {
  93. map.scene.globe.depthTestAgainstTerrain = false;
  94. },
  95. },
  96. {
  97. text: "视角切换",
  98. iconCls: "fa fa-street-view",
  99. children: [
  100. {
  101. text: "移动到此处",
  102. iconCls: "fa fa-send-o",
  103. show: function (e) {
  104. return Cesium.defined(e.cartesian);
  105. },
  106. callback: function (e) {
  107. let cameraDistance = Cesium.Cartesian3.distance(e.cartesian, map.camera.positionWC) * 0.1;
  108. map.flyToPoint(e.cartesian, {
  109. radius: cameraDistance, //距离目标点的距离
  110. maximumHeight: map.camera.positionCartographic.height,
  111. });
  112. },
  113. },
  114. ],
  115. },
  116. ];
  117. map.bindContextMenu(mapContextmenuItems);
  118. }
  119. //2.在layer图层上绑定右键菜单
  120. function bindLayerDemo() {
  121. var graphicLayer = new mars3d.layer.GeoJsonLayer({
  122. name: "标绘示例数据",
  123. url: "//data.mars3d.cn/file/geojson/mars3d-draw.json",
  124. });
  125. map.addLayer(graphicLayer);
  126. //在layer上绑定右键菜单
  127. graphicLayer.bindContextMenu([
  128. {
  129. text: "删除对象",
  130. iconCls: "fa fa-trash-o",
  131. callback: function (e) {
  132. let graphic = e.graphic;
  133. if (graphic) {
  134. graphicLayer.removeGraphic(graphic);
  135. }
  136. },
  137. },
  138. {
  139. text: "计算长度",
  140. iconCls: "fa fa-medium",
  141. show: function (e) {
  142. let graphic = e.graphic;
  143. return (
  144. graphic.type === "polyline" ||
  145. graphic.type === "curve" ||
  146. graphic.type === "polylineVolume" ||
  147. graphic.type === "corridor" ||
  148. graphic.type === "wall"
  149. );
  150. },
  151. callback: function (e) {
  152. let graphic = e.graphic;
  153. let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
  154. haoutil.alert("该对象的长度为:" + strDis);
  155. },
  156. },
  157. {
  158. text: "计算周长",
  159. iconCls: "fa fa-medium",
  160. show: function (e) {
  161. let graphic = e.graphic;
  162. return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
  163. },
  164. callback: function (e) {
  165. let graphic = e.graphic;
  166. let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
  167. haoutil.alert("该对象的周长为:" + strDis);
  168. },
  169. },
  170. {
  171. text: "计算面积",
  172. iconCls: "fa fa-reorder",
  173. show: function (e) {
  174. let graphic = e.graphic;
  175. return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
  176. },
  177. callback: function (e) {
  178. let graphic = e.graphic;
  179. let strArea = mars3d.MeasureUtil.formatArea(graphic.area);
  180. haoutil.alert("该对象的面积为:" + strArea);
  181. },
  182. },
  183. ]);
  184. }
  185. //3.在graphic数据上绑定右键菜单
  186. function bindGraphicDemo() {
  187. //创建矢量数据图层
  188. let graphicLayer = new mars3d.layer.GraphicLayer();
  189. map.addLayer(graphicLayer);
  190. let graphic = new mars3d.graphic.BoxEntity({
  191. position: new mars3d.LatLngPoint(116.336525, 31.196721, 323.35),
  192. style: {
  193. dimensions: new Cesium.Cartesian3(2000.0, 2000.0, 2000.0),
  194. fill: true,
  195. color: "#00ff00",
  196. opacity: 0.9,
  197. label: {
  198. text: "graphic绑定的演示",
  199. font_size: 25,
  200. font_family: "楷体",
  201. color: "#003da6",
  202. outline: true,
  203. outlineColor: "#bfbfbf",
  204. outlineWidth: 2,
  205. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  206. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  207. },
  208. },
  209. });
  210. graphicLayer.addGraphic(graphic);
  211. //2.在graphic上绑定右键菜单
  212. graphic.bindContextMenu([
  213. {
  214. text: "删除对象[graphic绑定的]",
  215. iconCls: "fa fa-trash-o",
  216. callback: function (e) {
  217. let graphic = e.graphic;
  218. if (graphic) {
  219. graphic.remove();
  220. }
  221. },
  222. },
  223. ]);
  224. }
  225. </script>
  226. </body>
  227. </html>