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

//历史停留
const historyLocation = {
    data: [],
    markers: {},
    infowins: {},
    bindEvent(){
        EventBus.$on('uiMap.historyLocation.draw', (data)=>{
            this.destroy();
            const MAP = mapService.map;
            this.data = data;

            // MAPGEO.transferAddressByGroup(this, ()=>{
            //     EventBus.$emit('uiMap.historyLocation.transferSuccess', data);
            //     this.data.forEach((item)=>{
            //         let marker = mapService.createHistoryLocationMarker([item.elng, item.elat], MAP);
            //         let infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item);
            //         this.markers[item.idx] = marker;
            //         this.infowins[item.idx] = infoWindow;
            //     });
            //     console.log(this.markers)
            //     mapService.setFitView(MAP);
            //     this.createClusterer();
            // });
            // v1.7.0历史停留为保证地址信息显示与导出一致 取后端返回的address 不需高德逆地址解析
            EventBus.$emit('uiMap.historyLocation.transferSuccess', data);
            this.data.forEach((item)=>{
                let marker = mapService.createHistoryLocationMarker([item.elng, item.elat], MAP);
                let infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item);
                this.markers[item.idx] = marker;
                this.infowins[item.idx] = infoWindow;
            });
            console.log(this.markers)
            mapService.setFitView(MAP);
            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);
                MAP.setZoomAndCenter(MAP.getZoom(), lnglat);
            }
            
        });
    },
    createClusterer(){
        var cluster, markers = [];
        for(let idx in this.markers){
            markers.push(this.markers[idx]);
        }
        
        var self = this;
        var map = mapService.map;

        
        var count = markers.length;
        var _renderClusterMarker = function (context) {
            var factor = Math.pow(context.count / count, 1 / 18);
            var div = document.createElement('div');
            var Hue = 180 - factor * 180;
            var bgColor = 'hsla(' + Hue + ',100%,50%,0.7)';
            var fontColor = 'hsla(' + Hue + ',100%,20%,1)';
            var borderColor = 'hsla(' + Hue + ',100%,40%,1)';
            var shadowColor = 'hsla(' + Hue + ',100%,50%,1)';
            div.style.backgroundColor = bgColor;
            var size = Math.round(30 + Math.pow(context.count / count, 1 / 5) * 20);
            div.style.width = div.style.height = size + 'px';
            div.style.border = 'solid 1px ' + borderColor;
            div.style.borderRadius = size / 2 + 'px';
            div.style.boxShadow = '0 0 1px ' + shadowColor;
            div.innerHTML = context.count;
            div.style.lineHeight = size + 'px';
            div.style.color = fontColor;
            div.style.fontSize = '14px';
            div.style.textAlign = 'center';
            context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
            context.marker.setContent(div)
        };

        addCluster(2);

        function addCluster(tag) {
            if (self.cluster) {
                self.cluster.setMap(null);
            }
            if (tag == 2) {//完全自定义
                self.cluster = new AMap.MarkerClusterer(map, markers, {
                    gridSize: 80,
                    maxZoom: 16,
                    renderClusterMarker: _renderClusterMarker
                });
            } else if (tag == 1) {//自定义图标
                var sts = [{
                    url: "https://a.amap.com/jsapi_demos/static/images/blue.png",
                    size: new AMap.Size(32, 32),
                    offset: new AMap.Pixel(-16, -16)
                }, {
                    url: "https://a.amap.com/jsapi_demos/static/images/green.png",
                    size: new AMap.Size(32, 32),
                    offset: new AMap.Pixel(-16, -16)
                }, {
                    url: "https://a.amap.com/jsapi_demos/static/images/orange.png",
                    size: new AMap.Size(36, 36),
                    offset: new AMap.Pixel(-18, -18)
                }, {
                    url: "https://a.amap.com/jsapi_demos/static/images/red.png",
                    size: new AMap.Size(48, 48),
                    offset: new AMap.Pixel(-24, -24)
                }, {
                    url: "https://a.amap.com/jsapi_demos/static/images/darkRed.png",
                    size: new AMap.Size(48, 48),
                    offset: new AMap.Pixel(-24, -24)
                }];
                self.cluster = new AMap.MarkerClusterer(map, markers, {
                    styles: sts,
                    gridSize: 80
                });
            } else {//默认样式
                self.cluster = new AMap.MarkerClusterer(map, markers, {gridSize: 80});
            }
        }
    },
    destroy: function(){
        const MAP = mapService.map;
        //MAP.clearMap();
        this.data = [];
        
        for(let idx in this.markers){
            MAP.remove(this.markers[idx])
        }
        for(let idx in this.infowins){
            MAP.remove(this.infowins[idx])
        }
        this.markers = {};
        this.infowins = {};
        if (this.cluster) {
            this.cluster.setMap(null);
            self.cluster = null;
        }
        this.clearTimeoutIds();
    },
    clearTimeoutIds(){
        this.timeoutIds && this.timeoutIds.forEach((item)=>{
            item && window.clearTimeout(item);
        });
        this.timeoutIds = []
    }
}

export default historyLocation;