arcgis.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <div class="arcgis"></div>
  3. </template>
  4. <script>
  5. import Map from "@arcgis/core/Map";
  6. import Basemap from "@arcgis/core/Basemap";
  7. import Graphic from "@arcgis/core/Graphic";
  8. import SpatialReference from "@arcgis/core/geometry/SpatialReference";
  9. import PictureMarkerSymbol from "@arcgis/core/symbols/PictureMarkerSymbol";
  10. import MapView from "@arcgis/core/views/MapView";
  11. import SceneView from "@arcgis/core/views/SceneView";
  12. import MapImageLayer from "@arcgis/core/layers/MapImageLayer";
  13. import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
  14. import IdentifyParameters from "@arcgis/core/tasks/support/IdentifyParameters";
  15. import "@arcgis/core/assets/esri/themes/light/main.css";
  16. const mapUrl = "http://61.161.152.110:12345/arcgis/rest/services/NX_MAP_15/MapServer"; // ArcMap地址
  17. // const mapUrl = "http://172.16.6.30:12345/arcgis/rest/services/NX_MAP_15/MapServer"; // ArcMap地址
  18. let basemap = null;
  19. let map = null;
  20. let view = null;
  21. let point_graphicsLayer = null;
  22. let line_graphicsLayer = null;
  23. let text_graphicsLayer = null;
  24. let point_graphics = [];
  25. let line_graphics = [];
  26. let text_graphics = [];
  27. export default {
  28. // 名称
  29. name: "arcgis",
  30. // 使用组件
  31. components: {},
  32. // 传入参数
  33. props: {
  34. data: {
  35. type: Object,
  36. default: () => {
  37. return {
  38. mode: "2D", // 模式 2D 3D
  39. title: "宁夏地图", // 标题
  40. center: [106.230909, 38.487193], // 初始中心点
  41. height: 654, // 3D地图初始相机高度
  42. tilt: 65, // 俯视角
  43. scale: 128000, // 缩放
  44. }
  45. },
  46. }
  47. },
  48. // 自定义事件
  49. emits: {
  50. when: null, // 地图加载完成
  51. clickMap: null, // 点击地图(可点击元素)
  52. },
  53. // 数据
  54. data() {
  55. return {
  56. fanStateImgMapping: [{
  57. name: "待机",
  58. code: "dj",
  59. img: require("@assets/map/fan/green.png")
  60. },
  61. {
  62. name: "运行",
  63. code: "yx",
  64. img: require("@assets/map/fan/blue.png")
  65. },
  66. {
  67. name: "限电",
  68. code: "xd",
  69. img: require("@assets/map/fan/purple.png")
  70. },
  71. {
  72. name: "故障",
  73. code: "gz",
  74. img: require("@assets/map/fan/red.png")
  75. },
  76. {
  77. name: "检查",
  78. code: "jc",
  79. img: require("@assets/map/fan/orange.png")
  80. },
  81. {
  82. name: "离线",
  83. code: "lx",
  84. img: require("@assets/map/fan/black.png")
  85. },
  86. {
  87. name: "受累",
  88. code: "sl",
  89. img: require("@assets/map/fan/white.png")
  90. },
  91. ],
  92. syzImg: require("@assets/temp.png"),
  93. }
  94. },
  95. // 函数
  96. methods: {
  97. // 初始化地图
  98. initMap: function() {
  99. basemap = new Basemap({
  100. // 底图
  101. baseLayers: [
  102. new MapImageLayer({
  103. url: mapUrl,
  104. title: this.data.title,
  105. spatialReference: SpatialReference.WGS84,
  106. }),
  107. ],
  108. title: this.data.title,
  109. spatialReference: SpatialReference.WGS84,
  110. });
  111. map = new Map({
  112. // 地图容器
  113. basemap: basemap,
  114. });
  115. if (this.data.mode == "3D") {
  116. view = new SceneView({
  117. // 3D显示图层
  118. map: map,
  119. center: this.data.center,
  120. camera: {
  121. position: {
  122. x: this.data.center[0], // lon
  123. y: this.data.center[1], // lat
  124. z: this.data.height, // elevation in meters
  125. },
  126. tilt: this.data.tilt,
  127. },
  128. container: this.$el,
  129. });
  130. } else {
  131. view = new MapView({
  132. // 3D显示图层
  133. map: map,
  134. center: this.data.center,
  135. container: this.$el,
  136. });
  137. }
  138. view.scale = this.data.scale;
  139. //去掉版权
  140. view.ui._removeComponents(["attribution"]);
  141. view.when(() => {
  142. this.when();
  143. this.$emit('when');
  144. });
  145. },
  146. when: function() {
  147. // 当地图加载完成
  148. point_graphicsLayer = new GraphicsLayer({
  149. graphics: []
  150. });
  151. line_graphicsLayer = new GraphicsLayer({
  152. graphics: []
  153. });
  154. text_graphicsLayer = new GraphicsLayer({
  155. graphics: []
  156. });
  157. map.add(line_graphicsLayer);
  158. map.add(point_graphicsLayer);
  159. map.add(text_graphicsLayer);
  160. view.on("click", (event) => {
  161. view.hitTest(event).then((response) => {
  162. let result = null;
  163. if (response.results.length) {
  164. const canClick = ["picture-marker", "text"];
  165. response.results.forEach(item => {
  166. if (canClick.indexOf(item.graphic.symbol.type) >= 0) {
  167. result = item.graphic.attributes;
  168. }
  169. })
  170. }
  171. if (result != null) {
  172. this.$emit('clickMap', result);
  173. }
  174. })
  175. })
  176. },
  177. // 通过name或code获取img
  178. getFanImg: function(nameOrState) {
  179. const item = this.fanStateImgMapping.find(t => t.name == nameOrState || t.code ==
  180. nameOrState);
  181. if (item) {
  182. return item.img;
  183. } else {
  184. console.error(nameOrState, "对应的图片不存在");
  185. return "";
  186. }
  187. },
  188. // 添加图片点
  189. addImgPoint: function(jsonItem) {
  190. let item = JSON.parse(JSON.stringify(jsonItem));
  191. item.geometry.type = "point";
  192. item.geometry.spatialReference = SpatialReference.WGS84;
  193. item.symbol = {
  194. type: "picture-marker",
  195. url: item.attributes.type == "升压站" ? this.syzImg : this.getFanImg("dj"),
  196. width: item.attributes.type == "升压站" ? "96px" : "48px",
  197. height: item.attributes.type == "升压站" ? "96px" : "48px",
  198. xoffset: 0,
  199. yoffset: 18
  200. };
  201. let graphic = new Graphic(item);
  202. point_graphics.push(graphic);
  203. point_graphicsLayer.add(graphic);
  204. this.addImgText(jsonItem);
  205. },
  206. // 添加图片文字
  207. addImgText: function(jsonItem) {
  208. let item = JSON.parse(JSON.stringify(jsonItem));
  209. item.geometry.type = "point";
  210. item.geometry.spatialReference = SpatialReference.WGS84;
  211. item.symbol = {
  212. type: "text",
  213. color: "white",
  214. haloColor: "black",
  215. haloSize: "1px",
  216. text: item.attributes.code,
  217. xoffset: 0,
  218. yoffset: -10,
  219. font: {
  220. size: 12,
  221. // family: "Josefin Slab",/
  222. weight: "bold"
  223. }
  224. };
  225. let graphic = new Graphic(item);
  226. text_graphics.push(graphic);
  227. text_graphicsLayer.add(graphic);
  228. },
  229. // 添加线段
  230. addLine: function(jsonItem) {
  231. let item = JSON.parse(JSON.stringify(jsonItem));
  232. item.geometry.type = "polyline";
  233. item.geometry.spatialReference = SpatialReference.WGS84;
  234. item.symbol = {
  235. type: "simple-line",
  236. color: "#05bb4c",
  237. width: item.attributes.width
  238. }
  239. let graphic = new Graphic(item);
  240. line_graphics.push(graphic);
  241. line_graphicsLayer.add(graphic);
  242. },
  243. addFanByJson: function(jsonObj, lineJsonObj) {
  244. jsonObj.forEach(item => {
  245. this.addImgPoint(item);
  246. });
  247. lineJsonObj.forEach(item => {
  248. this.addLine(item);
  249. });
  250. },
  251. goto: function(point) {
  252. view.goTo({
  253. center: point
  254. }).catch(function(error) {
  255. if (error.name != "AbortError") {
  256. console.error(error);
  257. }
  258. });
  259. }
  260. },
  261. // 生命周期钩子
  262. beforeCreate() {
  263. // 创建前
  264. },
  265. created() {
  266. // 创建后
  267. },
  268. beforeMount() {
  269. // 渲染前
  270. },
  271. mounted() {
  272. // 渲染后
  273. this.initMap();
  274. },
  275. beforeUpdate() {
  276. // 数据更新前
  277. },
  278. updated() {
  279. // 数据更新后
  280. },
  281. }
  282. </script>
  283. <style lang="less">
  284. .arcgis {
  285. width: 100%;
  286. height: 100%;
  287. }
  288. </style>