arcgis.vue 12 KB

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