北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
lixuliang
2024-03-08 ac755c3add30177675e189cf7cb637e1f09eb827
src/utils/tool.js
@@ -1,6 +1,7 @@
import _GLOBAL from '@/assets/GLOBAL2'
import gcj02Mecator from '@/utils/transform'
import { ceil } from 'lodash';
//清除
export function clearAll() {
    if (_GLOBAL.ImageLayer) {
@@ -118,6 +119,9 @@
//加载历史影像
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({
            url: `https://skyzt.bda.gov.cn/yzAdapter/Vector/?request=1&year=${layerName}&type=Sate&level={z}&x={x}&y={y}`,
@@ -126,19 +130,75 @@
    window.mapapi.addLayer(_GLOBAL.historyLayer);
}
//获取当前定位
//实时获取定位
export function getCurrentPosition() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((res) => {
            console.log(res);//这里会返回经纬度,然后还要通过经纬度转换地区名称
        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);
};