import _GLOBAL from '@/assets/GLOBAL2' import gcj02Mecator from '@/utils/transform' import { ceil } from 'lodash'; //清除 export function clearAll() { if (_GLOBAL.ImageLayer) { window.mapapi.removeLayer(_GLOBAL.ImageLayer); } if (_GLOBAL.annotation) { window.mapapi.removeLayer(_GLOBAL.annotation); } if (_GLOBAL.hexin) { window.mapapi.removeLayer(_GLOBAL.hexin); } if (_GLOBAL.yzxcgh) { window.mapapi.removeLayer(_GLOBAL.yzxcgh); } if (_GLOBAL.baseMap) { window.mapapi.removeLayer(_GLOBAL.baseMap); } if (_GLOBAL.geoMap) { window.mapapi.removeLayer(_GLOBAL.geoMap); } } //加载航拍影像 export function addImageLayer() { _GLOBAL.ImageLayer = new ol.layer.Tile({ source: new ol.source.XYZ({ url: `${'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'}/tile/{z}/{y}/{x}`, projection:"EPSG:3857", tileSize: 512 }), }); window.mapapi.addLayer(_GLOBAL.ImageLayer); } //加载白色地图 export function loadBaseMap() { _GLOBAL.baseMap = new ol.layer.Tile({ source: new ol.source.TileWMS({ ratio: 3, url: "https://skyzt.bda.gov.cn/gisserver/wmsserver/shiliangditu_baisexi_wms", //图层地址 params: { VERSION: "1.1.1", SRS: "EPSG:3857", FORMAT: "image/png", TILED: true, LAYERS: "", }, }), }); window.mapapi.addLayer(_GLOBAL.baseMap) } //加载高德地图 export function loadGeoMap() { _GLOBAL.geoMap = new ol.layer.Tile({ source: new ol.source.XYZ({ wrapX: false, projection: gcj02Mecator, url: ` https://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}`, }), }); window.mapapi.addLayer(_GLOBAL.geoMap); } //加载高德标注 export function addAnnotationLayer() { _GLOBAL.annotation = new ol.layer.Tile({ source: new ol.source.XYZ({ wrapX: false, projection: gcj02Mecator, url: `https://webst02.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}`, }), }); window.mapapi.addLayer(_GLOBAL.annotation); } // 核心区规划范围 export function addHxLayer() { _GLOBAL.hexin = new ol.layer.Tile({ source: new ol.source.TileWMS({ ratio: 3, url: 'https://skyzt.bda.gov.cn/gisserver/wmsserver/YZ_BJ60', params: { 'VERSION': '1.1.1', 'SRS': 'EPSG:3857', 'FORMAT': 'image/png', 'TILED': true, "LAYERS": '' } }) }) window.mapapi.addLayer(_GLOBAL.hexin) } //亦庄新城规划范围 export function addYzLayer() { _GLOBAL.yzxcgh = new ol.layer.Tile({ source: new ol.source.TileWMS({ ratio: 3, url: 'https://skyzt.bda.gov.cn/gisserver/wmsserver/YZ_BJ',//图层地址 params: { 'VERSION': '1.1.1', 'SRS': 'EPSG:3857', 'FORMAT': 'image/png', 'TILED': true, "LAYERS": '' } }) }) window.mapapi.addLayer(_GLOBAL.yzxcgh) } //加载历史影像 export function addHistoryLayer(name) { const layerName = name; if (_GLOBAL.historyLayer) { window.mapapi.removeLayer(_GLOBAL.historyLayer) } _GLOBAL.historyLayer = new ol.layer.Tile({ source: new ol.source.XYZ({ projection:"EPSG:3857", url: `https://skyzt.bda.gov.cn/yzAdapter/Vector/?request=1&year=${layerName}&type=Sate&level={z}&x={x}&y={y}`, }) }); window.mapapi.addLayer(_GLOBAL.historyLayer); } //实时获取定位 export function getCurrentPosition() { if (navigator.geolocation) { navigator.geolocation.watchPosition((res) => { let lat = res.coords.latitude; let lon = res.coords.longitude; if (_GLOBAL.GPSMarker) { window.mapapi.removeLayer(_GLOBAL.GPSMarker) } _GLOBAL.GPSMarker = createGPSMarker([lon, lat], _GLOBAL.rotate); window.mapapi.addLayer(_GLOBAL.GPSMarker) }); } } //创建当前定位点 export function createGPSMarker(position, rotate) { const GPSMarkerLayer = new ol.layer.Vector({ id: 'LocationPoint', name: '定位点', source: new ol.source.Vector({ features: [new ol.Feature({ geometry: new ol.geom.Point(position), })] }), style: new ol.style.Style({ image: new ol.style.Icon({ src: require('@/assets/img/collection/mark.png'), anchorOrigin: "top-left", anchorXUnits: "fraction", anchorYUnits: "fraction", offsetOrigin: "bottom-right", scale: 0.6, rotation: rotate, opacity: 1, }) }), zIndex: 9999, }); return GPSMarkerLayer }; //监听定位点跟随旋转 export function listenDirection() { window.addEventListener('deviceorientation', function (e) { if (_GLOBAL.GPSMarker) { _GLOBAL.rotate = e.alpha * (Math.PI / -180); _marker.setStyle( new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 0.5], anchorOrigin: 'top-left', src: require('@/assets/img/collection/mark.png'), rotation: _GLOBAL.rotate, anchorXUnits: "fraction", anchorYUnits: "fraction", scale: 0.5, opacity: 1 }) }) ) } }, false); };