当前位置:   article > 正文

ArcGIS API For JS 实现右键菜单栏的功能

arcgis api for js 实现右键菜单栏的功能

最近一直被一个问题所困扰,像百度地图、高德地图、这些地图api,都有一些借口供我们调用,最近做了还好几个关于这几个地图api和OL结合,遇到一个问题就是偏移的问题,一时间无法解决。好了不扯了进入正题,这个demo是arcgis api管网给出的demo,感觉写的挺好的,在这里加点注解分享一下。

先来张图:

一完整demo

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  6. <title>右键菜单栏</title>
  7. <link rel="stylesheet" href="https://js.arcgis.com/3.25/dijit/themes/claro/claro.css">
  8. <link rel="stylesheet" href="https://js.arcgis.com/3.25/esri/css/esri.css">
  9. <style>
  10. html, body {
  11. height: 100%;
  12. margin: 0;
  13. padding: 0;
  14. width: 100%;
  15. overflow: hidden;
  16. }
  17. div.insetType {
  18. color: #97F900;
  19. font-size: 24px;
  20. font-family: Rockwell, Georgia, "Times New Roman", Times, serif;
  21. padding-left: 25px;
  22. }
  23. </style>
  24. <script src="https://js.arcgis.com/3.25/"></script>
  25. <script>
  26. var map, editToolbar, ctxMenuForGraphics, ctxMenuForMap;
  27. var selected, currentLocation;
  28. require([
  29. "esri/map",
  30. "esri/geometry/Point",
  31. "esri/geometry/Polygon",
  32. "esri/toolbars/draw",
  33. "esri/toolbars/edit",
  34. "esri/symbols/SimpleMarkerSymbol",
  35. "esri/symbols/SimpleLineSymbol",
  36. "esri/symbols/SimpleFillSymbol",
  37. "esri/graphic",
  38. "esri/geometry/jsonUtils",
  39. "esri/Color",
  40. "dojo/parser",
  41. "dijit/Menu",
  42. "dijit/MenuItem",
  43. "dijit/MenuSeparator",
  44. "dijit/form/Button",
  45. "dijit/layout/BorderContainer",
  46. "dijit/layout/ContentPane",
  47. "dojo/domReady!"
  48. ], function (
  49. Map, Point, Polygon,
  50. Draw, Edit,
  51. SimpleMarkerSymbol, SimpleLineSymbol,
  52. SimpleFillSymbol,
  53. Graphic, geometryJsonUtils,
  54. Color, parser,
  55. Menu, MenuItem, MenuSeparator
  56. ) {
  57. parser.parse();
  58. map = new Map("map", {
  59. basemap: "satellite",
  60. center: [20.039, 62.739],
  61. zoom: 3
  62. });
  63. //地图加载事件
  64. map.on("load", createToolbarAndContextMenu);
  65. function createToolbarAndContextMenu() {
  66. // 添加图层
  67. addGraphics();
  68. // 创建一个编辑工具
  69. editToolbar = new Edit(map);
  70. //地图点击事件
  71. map.on("click", function (evt) {
  72. editToolbar.deactivate();
  73. });
  74. //添加点按钮
  75. createMapMenu();
  76. //添加主菜单栏
  77. createGraphicsMenu();
  78. }
  79. //创建点主菜单栏
  80. function createMapMenu() {
  81. ctxMenuForMap = new Menu({
  82. onOpen: function (box) {
  83. //当前点的位置
  84. currentLocation = getMapPointFromMenuPosition(box);
  85. editToolbar.deactivate();
  86. }
  87. });
  88. //添加子菜单栏
  89. ctxMenuForMap.addChild(new MenuItem({
  90. label: "添加点",
  91. onClick: function (evt) {
  92. var symbol = new SimpleMarkerSymbol(
  93. SimpleMarkerSymbol.STYLE_SQUARE,
  94. 30,
  95. new SimpleLineSymbol(
  96. SimpleLineSymbol.STYLE_SOLID,
  97. new Color([200, 235, 254, 0.9]),
  98. 2
  99. ), new Color([200, 235, 254, 0.5]));
  100. var graphic = new Graphic(geometryJsonUtils.fromJson(currentLocation.toJson()), symbol);
  101. map.graphics.add(graphic);
  102. }
  103. }));
  104. //小部件启动
  105. ctxMenuForMap.startup();
  106. //绑定父容器
  107. ctxMenuForMap.bindDomNode(map.container);
  108. }
  109. //编辑等右键菜单栏函数封装
  110. function createGraphicsMenu() {
  111. // Creates right-click context menu for GRAPHICS
  112. ctxMenuForGraphics = new Menu({});
  113. ctxMenuForGraphics.addChild(new MenuItem({
  114. label: "编辑",
  115. onClick: function () {
  116. if (selected.geometry.type !== "point") {
  117. editToolbar.activate(Edit.EDIT_VERTICES, selected);
  118. } else {
  119. alert("Not implemented");
  120. }
  121. }
  122. }));
  123. ctxMenuForGraphics.addChild(new MenuItem({
  124. label: "移动",
  125. onClick: function () {
  126. editToolbar.activate(Edit.MOVE, selected);
  127. }
  128. }));
  129. ctxMenuForGraphics.addChild(new MenuItem({
  130. label: "旋转",
  131. onClick: function () {
  132. if (selected.geometry.type !== "point") {
  133. editToolbar.activate(Edit.ROTATE | Edit.SCALE, selected);
  134. } else {
  135. alert("Not implemented");
  136. }
  137. }
  138. }));
  139. ctxMenuForGraphics.addChild(new MenuItem({
  140. label: "样式",
  141. onClick: function () {
  142. alert("Not implemented");
  143. }
  144. }));
  145. ctxMenuForGraphics.addChild(new MenuSeparator());
  146. ctxMenuForGraphics.addChild(new MenuItem({
  147. label: "删除",
  148. onClick: function () {
  149. map.graphics.remove(selected);
  150. }
  151. }));
  152. ctxMenuForGraphics.startup();
  153. //给每个graphics添加鼠标移入事件
  154. map.graphics.on("mouse-over", function (evt) {
  155. //获取当前鼠标悬浮的graphic
  156. selected = evt.graphic;
  157. // Let's bind to the graphic underneath the mouse cursor
  158. ctxMenuForGraphics.bindDomNode(evt.graphic.getDojoShape().getNode());
  159. console.log(evt.graphic.getDojoShape().getNode());
  160. });
  161. //给每个graphics添加鼠标移出事件
  162. map.graphics.on("mouse-out", function (evt) {
  163. ctxMenuForGraphics.unBindDomNode(evt.graphic.getDojoShape().getNode());
  164. });
  165. }
  166. // 确定弹窗的位置
  167. function getMapPointFromMenuPosition(box) {
  168. var x = box.x, y = box.y;
  169. switch (box.corner) {
  170. case "TR":
  171. x += box.w;
  172. break;
  173. case "BL":
  174. y += box.h;
  175. break;
  176. case "BR":
  177. x += box.w;
  178. y += box.h;
  179. break;
  180. }
  181. var screenPoint = new Point(x - map.position.x, y - map.position.y);
  182. return map.toMap(screenPoint);
  183. }
  184. //添加图形函数
  185. function addGraphics() {
  186. var polygonSymbol = new SimpleFillSymbol(
  187. SimpleFillSymbol.STYLE_SOLID,
  188. new SimpleLineSymbol(
  189. SimpleLineSymbol.STYLE_DOT,
  190. new Color([151, 249, 0, 0.8]),
  191. 3
  192. ),
  193. new Color([151, 249, 0, 0.45])
  194. );
  195. var polygon = new Polygon({
  196. "rings": [
  197. [
  198. [-4226661.916056009, 8496372.808143634],
  199. [-3835304.3312360067, 8731187.359035634],
  200. [-2269873.991956003, 9005137.668409634],
  201. [-1213208.5129420012, 8613780.083589634],
  202. [-1017529.7205320001, 8065879.464841632],
  203. [-1213208.5129420012, 7478843.087611631],
  204. [-2230738.233474003, 6891806.710381631],
  205. [-2935181.8861500043, 6735263.6764536295],
  206. [-3522218.263380006, 6891806.710381631],
  207. [-3952711.606682008, 7165757.01975563],
  208. [-4265797.674538009, 7283164.295201631],
  209. [-4304933.433020009, 7635386.121539632],
  210. [-4304933.433020009, 7674521.880021632],
  211. [-4226661.916056009, 8496372.808143634]
  212. ]
  213. ],
  214. "spatialReference": {
  215. "wkid": 102100
  216. }
  217. });
  218. var arrow = new Polygon({
  219. "rings": [
  220. [
  221. [9862211.137464028, 6617856.40100763],
  222. [8922952.933896024, 5522055.163511626],
  223. [8922952.933896024, 5991684.265295628],
  224. [6105178.323192019, 5991684.265295628],
  225. [6105178.323192019, 7087485.50279163],
  226. [8922952.933896024, 7087485.50279163],
  227. [8922952.933896024, 7557114.604575632],
  228. [9862211.137464028, 6617856.40100763]
  229. ]
  230. ],
  231. "spatialReference": {
  232. "wkid": 102100
  233. }
  234. });
  235. var triangle = new Polygon({
  236. "rings": [
  237. [
  238. [2426417.02588401, 8535508.566625634],
  239. [4304933.433020014, 12292541.380897645],
  240. [6183449.840156019, 8535508.566625634],
  241. [2426417.02588401, 8535508.566625634]
  242. ]
  243. ],
  244. "spatialReference": {
  245. "wkid": 102100
  246. }
  247. });
  248. map.graphics.add(new Graphic(polygon, polygonSymbol));
  249. map.graphics.add(new Graphic(arrow, polygonSymbol));
  250. map.graphics.add(new Graphic(triangle, polygonSymbol));
  251. }
  252. });
  253. </script>
  254. </head>
  255. <body class="claro" style="font-size: 0.75em;">
  256. <div data-dojo-type="dijit/layout/BorderContainer"
  257. data-dojo-props="design:'headline', gutters:false"
  258. style="width:100%;height:100%;">
  259. <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>
  260. </div>
  261. </body>
  262. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/834915
推荐阅读
相关标签
  

闽ICP备14008679号