wujingjing
2025-01-17 4e04b16f7bf497502508101c45ef6ff0209b13e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<template>
    <div class="relative bg-white">
        <div ref="containerRef" class="h-full"></div>
        <div ref="infoWindowRef" v-show="infoWindowIsShow">
            <div
                class="bg-white rounded-md text-nowrap flex flex-col w-fit border border-solid border-blue-600 relative z-[1]"
                style="padding: 12px"
            >
                <span
                    @click="closeInfoWindow"
                    class="guanbi absolute ywifont ywicon-guanbi right-[4px] top-[-2px] font-bold text-[#c3c3c3] cursor-pointer"
                ></span>
 
                <div class="space-x-4 flex w-full">
                    <div class="flex flex-col w-1/2 key-list">
                        <span
                            v-for="(col, index) in colsArray"
                            :key="index"
                            class="flex-items-center justify-start text-gray-600"
                            style="height: 20px"
                            >{{ col.title ?? '' }}</span
                        >
                    </div>
 
                    <div class="flex flex-col w-1/2 value-list" v-if="infoWindowMapRow">
                        <span
                            v-for="(col, index) in colsArray"
                            :key="index"
                            class="flex-items-center justify-end text-black"
                            style="height: 20px"
                            >{{ infoWindowMapRow[index] ?? '' }}</span
                        >
                    </div>
                </div>
            </div>
        </div>
        <LayerControl v-if="olMap" :olMap="olMap" class="absolute top-3 left-10 z-10" />
    </div>
</template>
 
<script setup lang="ts" name="BasicMap">
import type { Overlay } from 'ol';
import 'ol/ol.css';
 
import { computed, onMounted, ref, shallowRef } from 'vue';
import equipSelectPic from './img/equip-select.svg';
import equipPic from './img/equip.svg';
import LayerControl from './LayerControl.vue';
import { GaoDeSourceType, OLMap } from '/@/model/map/OLMap';
 
const props = withDefaults(
    defineProps<{
        data: any;
        config?: {
            sourceType: GaoDeSourceType;
            markerIsVisible: boolean;
        };
    }>(),
    {
        config: () => ({
            sourceType: GaoDeSourceType.Vector,
            markerIsVisible: true,
        }),
    }
);
const colsArray = computed(() => {
    return props.data.cols ?? [];
});
const emit = defineEmits(['markerClick', 'closeInfoWindow']);
 
const containerRef = ref<HTMLDivElement>(null);
const infoWindowRef = ref<HTMLDivElement>(null);
const infoWindowIsShow = ref(false);
const infoWindowMapRow = ref(null);
const olMap = shallowRef<OLMap>();
let infoWindowOverlay: Overlay;
let lastOverlay: Overlay;
const setMarkerIcon = (overlay: Overlay, icon: string) => {
    if (!overlay) return;
    const ele = overlay.getElement() as HTMLImageElement;
    if (!ele) return;
    ele.src = icon;
};
const showInfoWindow = (overlay: Overlay) => {
    const position = overlay.getPosition();
    const row = overlay.get('extData')?.value;
    if (!row) return;
    lastOverlay && setMarkerIcon(lastOverlay, equipPic);
    infoWindowIsShow.value = true;
    infoWindowOverlay.setPosition(position);
    infoWindowMapRow.value = row;
    setMarkerIcon(overlay, equipSelectPic);
    lastOverlay = overlay;
    emit('markerClick', row);
};
 
const closeInfoWindow = () => {
    infoWindowIsShow.value = false;
    infoWindowMapRow.value = null;
    setMarkerIcon(lastOverlay, equipPic);
    emit('closeInfoWindow');
};
const addMarkerLayer = () => {
    const map = props.data.map;
    if (map.pos_x == null && map.pos_y == null) return;
    const dataList = (props.data?.values ?? []).map((item) => {
        const x = item[map.pos_x];
        const y = item[map.pos_y];
 
        return {
            position: [x, y],
            // textColor: item.color,
            extData: {
                value: item,
                recordSetTable: props.data,
            },
        };
    });
    olMap.value.addMarkerLayer(dataList, {
        markerOpt: {
            icon: {
                url: equipPic,
                size: 30,
                selectUrl: equipSelectPic,
            },
            click(e, label, extData, position) {
                showInfoWindow(label);
            },
        },
        layerOpt: {
            // allowCollision:false
        },
    });
};
const initMap = () => {
    olMap.value = new OLMap({
        container: containerRef.value,
        sourceType: props.config?.sourceType,
        markerIsVisible: props.config?.markerIsVisible,
    });
    addMarkerLayer();
    infoWindowOverlay = olMap.value.createEleOverlay(infoWindowRef.value);
    olMap.value.map.addOverlay(infoWindowOverlay);
};
 
defineExpose({
    olMap: olMap,
});
 
onMounted(() => {
    initMap();
});
</script>
<style scoped lang="scss"></style>