北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
lixuliang
2024-03-13 fa05dfcce55b8589d7f76175700a92fe196dcd9b
src/utils/tool.js
@@ -30,9 +30,12 @@
    _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);
    // window.mapapi.addLayer(_GLOBAL.ImageLayer);
    layersArray.insertAt(1, _GLOBAL.ImageLayer);
}
//加载白色地图
@@ -43,14 +46,15 @@
            url: "https://skyzt.bda.gov.cn/gisserver/wmsserver/shiliangditu_baisexi_wms", //图层地址
            params: {
                VERSION: "1.1.1",
                SRS: "EPSG:4326",
                SRS: "EPSG:3857",
                FORMAT: "image/png",
                TILED: true,
                LAYERS: "",
            },
        }),
    });
    window.mapapi.addLayer(_GLOBAL.baseMap)
    // window.mapapi.addLayer(_GLOBAL.baseMap)
    layersArray.insertAt(1, _GLOBAL.baseMap);
}
@@ -63,8 +67,8 @@
            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);
    // window.mapapi.addLayer(_GLOBAL.geoMap);
    layersArray.insertAt(1, _GLOBAL.geoMap);
}
@@ -88,7 +92,7 @@
            url: 'https://skyzt.bda.gov.cn/gisserver/wmsserver/YZ_BJ60',
            params: {
                'VERSION': '1.1.1',
                'SRS': 'EPSG:4326',
                'SRS': 'EPSG:3857',
                'FORMAT': 'image/png',
                'TILED': true,
                "LAYERS": ''
@@ -106,7 +110,7 @@
            url: 'https://skyzt.bda.gov.cn/gisserver/wmsserver/YZ_BJ',//图层地址
            params: {
                'VERSION': '1.1.1',
                'SRS': 'EPSG:4326',
                'SRS': 'EPSG:3857',
                'FORMAT': 'image/png',
                'TILED': true,
                "LAYERS": ''
@@ -119,50 +123,36 @@
//加载历史影像
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);
}
//获取当前定位
var _rotate = 90;
var _marker = null;
var points = [];
export function randomPoint() {
    let t = 116.505348;
    let e = 39.795592;
    t += .01 * Math.random();
    e += .01 * Math.random();
    points.push([t, e]);
}
//实时获取定位
export function getCurrentPosition() {
    // setInterval(() => {
    //     randomPoint();
    // }, 1000)
    // console.log(points, '点位测试')
    // _marker = createGPSMarker(points, _rotate);
    // window.mapapi.addLayer(_marker)
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition((res) => {
            let lat = res.coords.latitude;
            let lon = res.coords.longitude;
            _marker = createGPSMarker([lon, lat], _rotate);
            window.mapapi.addLayer(_marker)
            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',
@@ -191,17 +181,18 @@
};
//监听定位点跟随旋转
export function listenDirection() {
    window.addEventListener('deviceorientation', function (e) {
        if (_marker) {
            _rotate = e.alpha * (Math.PI / -180);
        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: _rotate,
                        rotation: _GLOBAL.rotate,
                        anchorXUnits: "fraction",
                        anchorYUnits: "fraction",
                        scale: 0.5,