mapService.js 5.5 KB
//import ClipboardJS from 'clipboard'
//window.ClipboardJS = ClipboardJS
//import EventBus from '../event-bus'
import { Message } from "element-ui";
// EventBus.$on('uiMap.copySuccess', (data)=>{
//     Message({
//       type: 'success',
//       message: '已复制内容至粘贴板'
//     });
// });

function MapService() {
    this.map = null;
    this.restainLayer = null;
}

MapService.prototype = {

    // 设置默认图标
    setDefaultIcon(marker) {
        marker.setIcon(require('@/assets/images/icon_car.png'))
    },

    // 获取经纬度
    getLngLat: function (lng, lat) {
        return new AMap.LngLat(lng, lat);
    },

    // 清空地图覆盖物
    clearMap: function (map) {
        this.map.clearMap();
    },

    // 设置地图视角
    setFitView: function (map) {
        this.map.setFitView();
    },
    // 创建车辆位置点
    createCarMarker: function (lnglatXY, map) {
        var marker = new AMap.Marker({
            position: lnglatXY,
            icon: require('@/assets/images/icon_car.png'),
            map: map,
            autoRotation: true,
            offset: new AMap.Pixel(-18, -11),
            zIndex: 1020,
            animation: 'AMAP_ANIMATION_DROP'
        });
        return marker;
    },

    // 创建位置点弹框
    createActiveInfo(info) {
        let msg = '报警'
        // 激活
        if (info.type == 1) {
            msg = '激活'
        }

        // 定位
        if (info.type == 2) {
            msg = '定位'
        }
        let _html = `<h3 class="map-infowindow-title">${info.name}/${info.standNo}</h3>
        <div class="map-infowindow-ct">
            <p><i class="icon icon-info_device"></i> 设备号: <span class="des">${info.sn} (${info.source == 1 ? '有线' : '无线'})</span></p>`

        // 是否包含断电、光感报警
        if (info.alarmTypeSet && info.alarmTypeSet.length > 0) {
            let tg = '';
            let cHmtl = ''
            let typeList = {
                1: '断电',
                99: '光感'
            }
            info.alarmTypeSet.forEach((alarmType) => {
                tg += `<a class="btn_danger">${typeList[alarmType]}</a>`
            });
            cHmtl = '<p><i class="icon icon-info_device_alarm"></i><span>报警状态:</span>' + tg + '</p>';
            _html += cHmtl
        }

        _html += `<p><i class="icon icon-info_time"></i>${msg}时间: <span class="des"data-time>${info.time}</span></p>
        <p style="display:${info.bsLng ? 'flex' : 'none'}"><i class="icon icon-info_type"></i>定位类型: <a class="locate-btn active" id="locateTypeGps">GPS</a><a class="locate-btn" id="locateTypeBase">基站</a></p>
        <p class="long_p"><i class="icon icon-info_location"></i><span>${msg}地址:</span><span class="des" data-transferAddress>${info.address}</span></p>
    </div>`;
        return _html
    },

    // 位置点弹框
    bindActiveLocationInfoWindow(marker, map, info) {
        var self = this
        var infoDiv = document.createElement('div')
        infoDiv.innerHTML = self.createActiveInfo(info)

        // gps位置信息
        function fillGPS() {
            infoDiv.querySelector('[data-time]').textContent = info.time;
            infoDiv.querySelector('[data-transferAddress]').textContent = info.address;
            infoDiv.querySelector('#locateTypeGps').classList.add('active');
            infoDiv.querySelector('#locateTypeBase').classList.remove('active');
            marker.setPosition(self.getLngLat(info.lng, info.lat));
            marker.infoWindow && marker.infoWindow.open(map, marker.getPosition());
        }

        // 基站位置信息
        function fillBase() {
            infoDiv.querySelector('[data-time]').textContent = info.bsTime;
            infoDiv.querySelector('[data-transferAddress]').textContent = info.bsAddress;
            infoDiv.querySelector('#locateTypeGps').classList.remove('active');
            infoDiv.querySelector('#locateTypeBase').classList.add('active');
            marker.setPosition(self.getLngLat(info.bsLng, info.bsLat));
            marker.infoWindow && marker.infoWindow.open(map, marker.getPosition());
        }

        infoDiv.querySelector('#locateTypeGps').onclick = function (e) {
            fillGPS()
        }
        infoDiv.querySelector('#locateTypeBase').onclick = function (e) {
            fillBase()
        }

        if (info.lng) {
            fillGPS()
        }

        if (!info.lng && info.bsLng) {
            fillBase()
        }

        return this.createActiveInfoWindow(marker, map, infoDiv)
    },

    // 创建位置点弹框
    createActiveInfoWindow(marker, map, div, isInfoOpen, isInfoAutoMove) {
        var infoWindow = new AMap.InfoWindow({
            offset: new AMap.Pixel(0, 0),
            content: div,
            autoMove: !!isInfoAutoMove,
        })
        console.log(infoWindow)
        marker.infoWindow = infoWindow
        if (isInfoOpen) {
            infoWindow.open(map, marker.getPosition())
        }

        //给Marker绑定单击事件
        marker.on('click', markerClick)

        function markerClick(e) {
            //infoWindow.setContent(e.target.content);
            infoWindow.open(map, e.target.getPosition())
        }
        return infoWindow
    },

    // 打开激活位置点弹框
    openActiveInfoWindow(map, makers, infoWindow) {
        const MAP = map
        const lnglat = makers.getPosition()
        infoWindow.open(MAP, lnglat)
        MAP.setZoomAndCenter(MAP.getZoom(), lnglat)
    }
}

const mapService = new MapService();

export default mapService