干燥机配套车间生产管理系统/云平台前端
baoshiwei
2024-12-06 346c9a61f4407be8180aff944bf75bed9c5efd00
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<template>
    <div class="home_div">
        <div id="container" style="height: 100%; width: 100%"></div>
    </div>
</template>
<script setup lang="ts">
    import AMapLoader from '@amap/amap-jsapi-loader'
    import { listAllTenant } from '../../system/tenant/tenant.api'
    import { onMounted, ref } from 'vue'
  import { router } from '/@/router'
  const tenants = ref([])
    const points = ref([])
 
    const initMap = () => {
        window._AMapSecurityConfig = {
            securityJsCode: 'c1fa6ceb212b62bf9489da9c9986eb5e',
        }
        AMapLoader.load({
            key: '1582675884a1ef3c90b5ae1769c765a7', // 申请好的Web端开发者Key,首次调用 load 时必填
            version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
            plugins: ['AMap.Scale'], //需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
        }).then((AMap) => {
            var map = new AMap.Map('container', {
                viewMode: '2D', //  是否为3D地图模式
                zoom: 5, //  地图显示的缩放级别
                zooms: [2, 22], // 地图缩放范围
                // center: arr, //  地图中心点坐标
                layers: [new AMap.createDefaultLayer()], //设置图层,可设置成包含一个或多个图层的数组
                mapStyle: 'amap://styles/whitesmoke', //设置地图的显示样式
                resizeEnable: true, //  是否监控地图容器尺寸变化
            })
 
            var clusterIndexSet = {
                //定义点聚合规则
                city: {
                    minZoom: 2,
                    maxZoom: 9,
                },
                district: {
                    minZoom: 9,
                    maxZoom: 11,
                },
                building: {
                    minZoom: 11,
                    maxZoom: 22,
                },
            }
 
            function getStyle(context) {
                var clusterData = context.clusterData // 聚合中包含数据
                var index = context.index // 聚合的条件
                var count = context.count // 聚合中点的总数
                var marker = context.marker // 聚合绘制点 Marker 对象
                var color = ['66,130,198', '107,174,214', '78,200,211']
                var indexs = ['city', 'district', 'building']
                var i = indexs.indexOf(index['mainKey'])
                var text = clusterData[0][index['mainKey']]
                var size = Math.round(30 + Math.pow(count / points.value.length, 1 / 5) * 70)
                if (i <= 1) {
                    var extra = '<span class="showCount">' + context.count + '家</span>'
                    text = '<span class="showName">' + text + '</span>'
                    text += extra
                } else {
                    size = 12 * text.length + 20
                }
                var style = {
                    bgColor: 'rgba(' + color[i] + ',.8)',
                    borderColor: 'rgba(' + color[i] + ',1)',
                    text: text,
                    size: size,
                    index: i,
                    color: '#ffffff',
                    textAlign: 'center',
                    boxShadow: '0px 0px 3px rgba(0,0,0,0.6)',
                }
                return style
            }
            function getPosition(context) {
                var key = context.index.mainKey
                var dataItem = context.clusterData && context.clusterData[0]
                var districtName = dataItem[key]
                if (!district[districtName]) {
                    return null
                }
                var center = district[districtName].center.split(',')
                var centerLnglat = new AMap.LngLat(center[0], center[1])
                return centerLnglat
            }
            function _customRender(data) {
                const keys = Object.keys(data.clusterData)
                let markers = []
                for (var i = 0; i < keys.length; i++) {
                    var key = keys[i]
                    var cluster = data.clusterData[key]
                    var position = cluster.data[0].lnglat
                    var marker = new AMap.LabelMarker({
                        position: position,
                        text: {
                            content: cluster.data.length.toString(),
                            style: {
                                fillColor: '#ffffff',
                            },
                        },
                    })
                    markers.push(marker)
                }
                return {
                    type: 'type',
                    layer: null,
                    markers: markers,
                }
            }
 
            //聚合点样式
            // var _renderClusterMarker = function (context) {
            //     //context 为回调参数,
            //   console.log("context", context)
            //     //包含如下属性 marker:当前聚合点,count:当前聚合点内的点数量
            //     var clusterCount = context.count //聚合点内点数量
            //     context.marker.setContent('<div style="background-color:#00ff00">' + clusterCount + '</div>')
            // }
            // 自定义聚合点样式
            function _renderClusterMarker(context) {
                var clusterData = context.clusterData // 聚合中包含数据
                var index = context.index // 聚合的条件
                var count = context.count // 聚合中点的总数
                var marker = context.marker // 聚合点标记对象
                var styleObj = getStyle(context)
                // 自定义点标记样式
                var div = document.createElement('div')
                div.className = 'amap-cluster'
                div.style.backgroundColor = styleObj.bgColor
                div.style.width = styleObj.size + 'px'
                if (styleObj.index <= 1) {
                    div.style.height = styleObj.size + 'px'
                    // 自定义点击事件
                    context.marker.on('click', function (e) {
                        console.log(e)
                        var curZoom = map.getZoom()
                        if (curZoom < 9) {
                            curZoom = 9
                        } else if (curZoom < 11) {
              curZoom = 11
            }
                        map.setZoomAndCenter(curZoom, e.lnglat)
                    })
                } else {
          // 自定义点击事件
          context.marker.on('click', function (e) {
            let innerText = e.target.dom.innerText;
            tenants.value.forEach(item => {
              if(item.companyAddress.includes(innerText)) {
                //router.push({ path: '/tongjitang/bigworkshop', params: { tenant: item.id } })
                // router.push( {path: '/tongjitang/bigworkshop/' + item.id} )
                // router.push( '/tongjitang/bigworkshop/' + item.id )
                let href = '/tongjitang/bigworkshop/' + item.id;
                window.open(href, '_blank');
              }
            })
          })
          console.log(router)
        }
                div.style.border = 'solid 1px ' + styleObj.borderColor
                div.style.borderRadius = styleObj.size + 'px'
                div.innerHTML = styleObj.text
                div.style.color = styleObj.color
                div.style.textAlign = styleObj.textAlign
                div.style.boxShadow = styleObj.boxShadow
                context.marker.setContent(div)
                // 自定义聚合点标记显示位置
                // var position = getPosition(context);
                // if(position){
                //   context.marker.setPosition(position);
                // }
                context.marker.setAnchor('center')
            }
 
            map.plugin(['AMap.IndexCluster'], function () {
                console.log('cluster', points.value)
                var indexCluster = new AMap.IndexCluster(map, points.value, {
                    renderClusterMarker: _renderClusterMarker, //自定义聚合点样式
                    clusterIndexSet: clusterIndexSet, //聚合规则
                })
            })
        })
    }
 
    const getTenants = () => {
        listAllTenant().then((res) => {
      tenants.value = res
            res.forEach((item) => {
                let point = JSON.parse(item.companyAddress)
                points.value.push(point)
            })
            initMap()
            //console.log('points', points.value)
        })
    }
 
    getTenants()
    onMounted(() => {})
</script>
<style>
.amap-cluster {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
}
.showName {
  font-size: 14px;
}
.showCount,
.showName {
  display: block;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  width: 80%;
}
</style>
<style scoped lang="less">
    .home_div {
        height: 100%;
        width: 100%;
        padding: 0px;
        margin: 0px;
        position: relative;
    }
 
</style>