123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <template>
- <div class="arcgis">
- <div id="viewDiv"></div>
- <div id="marker">
- <div v-for="(marker, index) of markers" :key="index" v-show="marker.show"
- :id="'arcgic-marker-' + marker.slot" class="arcgis-marker" :style="'left: '+marker.x+'px; top: '+marker.y+'px;'">
- <slot :name="marker.slot"></slot>
- </div>
- </div>
- </div>
- </template>
- <script>
- import Map from "@arcgis/core/Map";
- import Basemap from "@arcgis/core/Basemap";
- import Graphic from "@arcgis/core/Graphic";
- import SpatialReference from "@arcgis/core/geometry/SpatialReference";
- import MapView from "@arcgis/core/views/MapView";
- import SceneView from "@arcgis/core/views/SceneView";
- import MapImageLayer from "@arcgis/core/layers/MapImageLayer";
- import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
- import SimpleMarkerSymbol from "@arcgis/core/symbols/SimpleMarkerSymbol";
- import "@arcgis/core/assets/esri/themes/light/main.css";
- const mapUrl = "http://61.161.152.110:12345/arcgis/rest/services/NX_MAP_15/MapServer"; // ArcMap地址
- // const mapUrl = "http://172.16.6.30:12345/arcgis/rest/services/NX_MAP_15/MapServer"; // ArcMap地址
- let basemap = null;
- let map = null;
- let view = null;
- let point_graphicsLayer = null;
- let line_graphicsLayer = null;
- let text_graphicsLayer = null;
- let img_graphicsLayer = null;
- let html_graphicsLayer = null;
- let point_graphics = [];
- let line_graphics = [];
- let text_graphics = [];
- let img_graphics = [];
- let html_graphics = [];
- export default {
- // 名称
- name: "arcgis",
- // 使用组件
- components: {},
- // 传入参数
- props: {
- data: {
- type: Object,
- default: () => {
- return {
- mode: "2D", // 模式 2D 3D
- title: "宁夏地图", // 标题
- center: [106.230909, 38.487193], // 初始中心点
- height: 654, // 3D地图初始相机高度
- tilt: 65, // 俯视角
- scale: 128000, // 缩放
- }
- },
- }
- },
- // 自定义事件
- emits: {
- when: null, // 地图加载完成
- clickMap: null, // 点击地图(可点击元素)
- },
- // 数据
- data() {
- return {
- fanStateImgMapping: [{
- name: "待机",
- code: "dj",
- img: require("@assets/map/fan/green.png")
- },
- {
- name: "运行",
- code: "yx",
- img: require("@assets/map/fan/blue.png")
- },
- {
- name: "限电",
- code: "xd",
- img: require("@assets/map/fan/purple.png")
- },
- {
- name: "故障",
- code: "gz",
- img: require("@assets/map/fan/red.png")
- },
- {
- name: "检查",
- code: "jc",
- img: require("@assets/map/fan/orange.png")
- },
- {
- name: "离线",
- code: "lx",
- img: require("@assets/map/fan/black.png")
- },
- {
- name: "受累",
- code: "sl",
- img: require("@assets/map/fan/white.png")
- },
- ],
- syzImg: require("@assets/temp.png"),
- markers: [],
- }
- },
- // 函数
- methods: {
- // 初始化地图
- initMap: function() {
- basemap = new Basemap({
- // 底图
- baseLayers: [
- new MapImageLayer({
- url: mapUrl,
- title: this.data.title,
- spatialReference: SpatialReference.WGS84,
- }),
- ],
- title: this.data.title,
- spatialReference: SpatialReference.WGS84,
- });
- map = new Map({
- // 地图容器
- basemap: basemap,
- });
- if (this.data.mode == "3D") {
- view = new SceneView({
- // 3D显示图层
- map: map,
- center: this.data.center,
- camera: {
- position: {
- x: this.data.center[0], // lon
- y: this.data.center[1], // lat
- z: this.data.height, // elevation in meters
- },
- tilt: this.data.tilt,
- },
- container: this.$el.querySelector("#viewDiv"),
- });
- } else {
- view = new MapView({
- // 3D显示图层
- map: map,
- center: this.data.center,
- container: this.$el.querySelector("#viewDiv"),
- });
- }
- view.scale = this.data.scale;
- //去掉版权
- view.ui._removeComponents(["attribution"]);
- view.when(() => {
- this.when();
- this.$emit('when');
- });
- },
- when: function() {
- // 当地图加载完成
- point_graphicsLayer = new GraphicsLayer({
- graphics: []
- });
- line_graphicsLayer = new GraphicsLayer({
- graphics: []
- });
- text_graphicsLayer = new GraphicsLayer({
- graphics: []
- });
- img_graphicsLayer = new GraphicsLayer({
- graphics: []
- });
- html_graphicsLayer = new GraphicsLayer({
- graphics: []
- });
- map.add(line_graphicsLayer);
- map.add(point_graphicsLayer);
- map.add(text_graphicsLayer);
- map.add(img_graphicsLayer);
- map.add(html_graphicsLayer);
- view.on("click", (event) => {
- view.hitTest(event).then((response) => {
- let result = null;
- if (response.results.length) {
- const canClick = ["picture-marker", "text"];
- response.results.forEach(item => {
- if (canClick.indexOf(item.graphic.symbol.type) >= 0) {
- result = item.graphic.attributes;
- }
- })
- }
- if (result != null) {
- this.$emit('clickMap', result);
- }
- })
- });
- view.watch("extent", (evt) => {
- this.markers.forEach((marker, index) => {
- let graphics = html_graphics[index];
- let screenPoint = view.toScreen(graphics.geometry);
- marker.x = screenPoint.x - marker.ox;
- marker.y = screenPoint.y - marker.oy;
- if (evt.height < 0.045) {
- if (!marker.show) {
- marker.show = true;
- graphics.symbol = new SimpleMarkerSymbol({
- color: "transparent",
- outline: {
- color: [255, 255, 255],
- width: 0
- }
- });
- }
- } else {
- if (marker.show) {
- marker.show = false;
- graphics.symbol = new SimpleMarkerSymbol({
- color: [5,187,76,0.75],
- outline: {
- color: [5,187,76],
- width: 2
- }
- });
- }
- }
- });
- });
- },
- // 通过name或code获取img
- getFanImg: function(nameOrState) {
- const item = this.fanStateImgMapping.find(t => t.name == nameOrState || t.code ==
- nameOrState);
- if (item) {
- return item.img;
- } else {
- console.error(nameOrState, "对应的图片不存在");
- return "";
- }
- },
- // 添加html
- addHtmlPoint: function (point, slot, ox=0, oy=0) {
- let item = {
- geometry: {
- type: "point",
- spatialReference: SpatialReference.WGS84,
- x: point[0],
- y: point[1],
- },
- symbol: {
- type: "simple-marker",
- color: "transparent",
- outline: {
- color: [255, 255, 255],
- width: 0
- }
- },
- // symbol: null,
- attributes: {},
- popupTemplate: null
- };
- let graphic = new Graphic(item);
- html_graphics.push(graphic);
- html_graphicsLayer.add(graphic);
- let screenPoint = view.toScreen(graphic.geometry);
- let marker = {
- slot: slot,
- ox: ox,
- oy: oy,
- x: screenPoint.x - ox,
- y: screenPoint.y - oy,
- show: true,
- };
- this.markers.push(marker);
- return marker;
- },
- // 添加图片点
- addImagePoint: function (point, src, width, height) {
- let item = {
- geometry: {
- type: "point",
- spatialReference: SpatialReference.WGS84,
- x: point[0],
- y: point[1]
- },
- symbol: {
- type: "picture-marker",
- url: this.tempImg,
- width: width,
- height: height,
- xoffset: 0,
- yoffset: 0
- },
- attributes: {},
- popupTemplate: null
- };
- console.log(item)
- let graphic = new Graphic(item);
- img_graphics.push(graphic);
- img_graphicsLayer.add(graphic);
- },
- // 移除
- remove: function (type, obj) {
- if (type == "html") {
- let markerIndex = this.markers.findIndex(t => t.slot == obj.slot);
- // let marker = this.markers[markerIndex];
- this.markers.splice(markerIndex, 1);
- let graphic = html_graphics[markerIndex];
- html_graphics.splice(markerIndex, 1);
- html_graphicsLayer.remove(graphic);
- } else {
- console.info("arcgis: No type:", type)
- }
- },
- // 添加图片点
- addImgPoint: function(jsonItem) {
- let item = JSON.parse(JSON.stringify(jsonItem));
- item.geometry.type = "point";
- item.geometry.spatialReference = SpatialReference.WGS84;
- item.symbol = {
- type: "picture-marker",
- url: item.attributes.type == "升压站" ? this.syzImg : this.getFanImg(item.attributes.state),
- width: item.attributes.type == "升压站" ? "96px" : "48px",
- height: item.attributes.type == "升压站" ? "96px" : "48px",
- xoffset: 0,
- yoffset: 18
- };
- let graphic = new Graphic(item);
- point_graphics.push(graphic);
- point_graphicsLayer.add(graphic);
- this.addImgText(jsonItem);
- },
- // 添加图片文字
- addImgText: function(jsonItem) {
- let item = JSON.parse(JSON.stringify(jsonItem));
- item.geometry.type = "point";
- item.geometry.spatialReference = SpatialReference.WGS84;
- item.symbol = {
- type: "text",
- color: "white",
- haloColor: "black",
- haloSize: "1px",
- text: item.attributes.code,
- xoffset: 0,
- yoffset: -10,
- font: {
- size: 12,
- // family: "Josefin Slab",/
- weight: "bold"
- }
- };
- let graphic = new Graphic(item);
- text_graphics.push(graphic);
- text_graphicsLayer.add(graphic);
- },
- // 添加线段
- addLine: function(jsonItem) {
- let item = JSON.parse(JSON.stringify(jsonItem));
- item.geometry.type = "polyline";
- item.geometry.spatialReference = SpatialReference.WGS84;
- item.symbol = {
- type: "simple-line",
- color: "#05bb4c",
- width: item.attributes.width
- }
- let graphic = new Graphic(item);
- line_graphics.push(graphic);
- line_graphicsLayer.add(graphic);
- },
- addFanByJson: function(jsonObj, lineJsonObj) {
- jsonObj.forEach(item => {
- this.addImgPoint(item);
- });
- lineJsonObj.forEach(item => {
- this.addLine(item);
- });
- },
- goto: function(point) {
- view.goTo({
- center: point
- }).catch(function(error) {
- if (error.name != "AbortError") {
- console.error(error);
- }
- });
- }
- },
- // 生命周期钩子
- beforeCreate() {
- // 创建前
- },
- created() {
- // 创建后
- },
- beforeMount() {
- // 渲染前
- },
- mounted() {
- // 渲染后
- this.initMap();
- },
- beforeUpdate() {
- // 数据更新前
- },
- updated() {
- // 数据更新后
- },
- }
- </script>
- <style lang="less">
- .arcgis {
- width: 100%;
- height: 100%;
- position: relative;
- overflow: hidden;
- #viewDiv {
- position: absolute;
- z-index: 1;
- width: 100%;
- height: 100%;
- top: 0;
- left: 0;
- }
- #marker {
- position: absolute;
- z-index: 2;
- width: 0;
- height: 0;
- top: 0;
- left: 0;
- }
- .arcgis-marker {
- position: absolute;
- z-index: 2;
- }
- }
- </style>
|