historyLocation.js 3.25 KB
import EventBus from '../event-bus'
import mapService from './mapBService.js'
import MAPGEO from './mapgeo'

//历史停留
const historyLocation = {
    data: [],
    points:[],
    cluster:'',
    markers: {},
    infowins: {},
    bindEvent(){
        EventBus.$on('uiMap.historyLocation.draw', (data)=>{
            this.destroy();
            this.data = data;
            let markers=[]
            // v1.7.0历史停留为保证地址信息显示与导出一致 取后端返回的address 不需高德逆地址解析
            EventBus.$emit('uiMap.historyLocation.transferSuccess', data);
            this.data.forEach((item)=>{
                let lnglatXY=mapService.getLngLat(item.blng, item.blat);
                markers.push(lnglatXY)
                let marker = mapService.createHistoryLocationMarker(lnglatXY, AMap);
                let infoWindow = mapService.bindLocationInfoWindow(marker, AMap, item);
                this.markers[item.idx] = marker;
                this.infowins[item.idx] = infoWindow;
                // AMap.invoke('addOverlay',this.markers[item.idx])
            });
           
            AMap.invoke('setViewport',markers)
            this.points=markers
            this.createClusterer();
        });

        EventBus.$on('uiMap.historyLocation.destroy', ()=>{
            this.destroy();
        });

        EventBus.$on('uiMap.historyLocation.openInfoWindow', (idx)=>{
            if(this.markers[idx]){
                const MAP = mapService.map;
                const lnglat = this.markers[idx].getPosition();
                // this.infowins[idx].open(MAP, lnglat);
                AMap.invoke("mapOpenInfoWindow",  this.infowins[idx],lnglat)
                AMap.invoke('centerAndZoom',lnglat,10)
            }
            
        });
    },
    createClusterer(){
        let map=mapService.mapInstance;
        var cluster, markers = [];

        for(let idx in this.markers){
            markers.push(this.markers[idx]);
        }
        this.cluster = AMap.invoke('Cluster', markers, data => {
            return {
                point: [data.latLng.lng, data.latLng.lat],
                properties: {
                marker: data
              }
            }
          })
         
          AMap.invoke('clusterSetData', this.cluster)
          AMap.invoke('clusterAddEventListener', 'click', (e) => {
            AMap.invoke('infoWindowDisableCloseOnClick', e.properties.marker.openInfoWindow)
            AMap.invoke("mapOpenInfoWindow", e.properties.marker.openInfoWindow,e.properties.marker.getPosition() )
           
        
        })
    },
    destroy: function(){
        const MAP = mapService.map;
        this.data = [];
       
        for(let idx in this.markers){
            AMap.invoke('removeOverlay',this.markers[idx])
        }
        for(let idx in this.infowins){
            AMap.invoke('mapCloseInfoWindow')
        }
        this.markers = {};
        this.infowins = {};
        if (this.cluster) {
            AMap.invoke('clusterDestroy')
            this.cluster = null;
        }
        this.clearTimeoutIds();
    },
    clearTimeoutIds(){
        this.timeoutIds && this.timeoutIds.forEach((item)=>{
            item && window.clearTimeout(item);
        });
        this.timeoutIds = []
    }
}

export default historyLocation;