Files
dvcp_v2_wxcp_app/src/project/xicheng/AppMerchantMap/AppMerchantMap.vue

431 lines
11 KiB
Vue
Raw Normal View History

2022-07-01 14:31:08 +08:00
<template>
<div class="AppMerchantMap">
2022-07-02 11:36:13 +08:00
<div class="search">
<image :src="$cdn + 'xincheng/search.png'" />
2022-07-02 18:51:25 +08:00
<input placeholder="请输入店主姓名、店名" v-model="businessName" @input="getList(true)">
2022-07-01 14:31:08 +08:00
</div>
2022-07-02 18:09:24 +08:00
<div class="search-list" v-if="isShow">
<div class="item border" v-for="(item, index) in list" :key="index" @click="toDetail(item)">
<div class="item-content">
<h3>{{ item.businessName }} - {{ item.bossName }}</h3>
<p>{{ item.businessAddress }}</p>
</div>
</div>
</div>
<div class="map-content" @click.stop="isShow = false">
2022-07-02 11:36:13 +08:00
<AiTMap :map.sync="map" :lib.sync="lib" :ops="ops" :libraries="['service', 'tools']"/>
2022-07-01 14:31:08 +08:00
</div>
</div>
</template>
<script>
2022-07-02 11:36:13 +08:00
import {mapState} from 'vuex'
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
export default {
name: 'AppMerchantMap',
appName: '商户地图',
data() {
return {
areaId: '',
lib: null,
map: null,
polygons: [],
labels: [],
businessName: '',
2022-07-07 10:31:54 +08:00
ops: {},
2022-07-02 11:36:13 +08:00
markers: [],
2022-07-02 18:09:24 +08:00
isShow: false,
MarkerCluster: null,
list: []
2022-07-02 11:36:13 +08:00
}
},
computed: {...mapState(['user', 'config'])},
created () {
this.areaId = this.user.areaId
this.getList()
2022-07-01 14:31:08 +08:00
},
2022-07-02 11:36:13 +08:00
mounted () {
this.initMap()
},
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
methods: {
2022-07-02 18:09:24 +08:00
toDetail (item) {
if (!item.lat) {
return this.$u.toast('该商户尚未标记')
}
2022-07-02 18:15:27 +08:00
this.businessName = item.businessName
2022-07-02 18:09:24 +08:00
this.isShow = false
this.map.easeTo({
center: new this.lib.LatLng(item.lat, item.lng),
2022-07-07 10:31:54 +08:00
zoom: 20
2022-07-02 18:09:24 +08:00
}, {
2022-07-07 10:31:54 +08:00
duration: 800
2022-07-02 18:09:24 +08:00
})
},
getList (isSearch) {
2022-07-02 11:36:13 +08:00
this.$http.post('/app/appcompany/list', null, {
params: {
2022-07-02 18:09:24 +08:00
size: isSearch ? 20 : 100000,
2022-07-02 11:36:13 +08:00
current: 1,
businessName: this.businessName
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
}).then((res) => {
if (res.code == 0) {
const markers = res.data.records.filter(item => item.lat).map(item => {
return {
2022-07-02 18:09:24 +08:00
...item,
title: item.businessName + '-' + item.bossName,
2022-07-02 16:17:55 +08:00
lnglat: [item.lng, item.lat],
2022-07-02 11:36:13 +08:00
lat: item.lat,
2022-07-02 16:17:55 +08:00
lng: item.lng,
2022-07-02 11:36:13 +08:00
id: item.id,
2022-07-02 18:09:24 +08:00
name: item.businessName + '-' + item.bossName
2022-07-02 11:36:13 +08:00
}
})
2022-07-01 14:31:08 +08:00
2022-07-02 18:09:24 +08:00
if (isSearch) {
this.list = res.data.records
this.isShow = true
} else {
this.getMarkerCluster(markers)
}
2022-07-02 11:36:13 +08:00
}
})
},
getMarkerCluster(points, count = 0) {
let {lib: TMap, map} = this
if (map) {
if (this.markers.length) {
this.markers.forEach(v => {
v.setMap(null)
})
}
class ClusterBubble extends TMap.DOMOverlay {
constructor(options) {
super(options);
}
onInit(options) {
this.content = options.content;
this.position = options.position;
this.customClass = options.customClass
};
onDestroy() {
this.dom.removeEventListener('click', this.onClick);
this.removeAllListeners();
}
createDOM() {
let dom = document.createElement('div');
dom.classList.add(this.customClass || 'marker');
dom.innerText = this.content;
// 监听点击事件实现zoomOnClick
this.onClick = this.onClick.bind(this);
// pc端注册click事件移动端注册touchend事件
dom.addEventListener('click', this.onClick);
dom.addEventListener('touchend', this.onClick);
return dom;
};
updateDOM() {
if (!this.map) {
return;
}
// 经纬度坐标转容器像素坐标
let pixel = this.map.projectToContainer(this.position);
// 使文本框中心点对齐经纬度坐标点
let left = pixel.getX() - this.dom.clientWidth / 2 + 'px';
let top = pixel.getY() - this.dom.clientHeight / 2 + 'px';
this.dom.style.transform = `translate(${left}, ${top})`;
this.emit('dom_updated');
};
onClick() {
this.emit('click');
}
}
this.MarkerCluster = new TMap.MarkerCluster({
map, gridSize: 60,
enableDefaultStyle: false, // 关闭默认样式
geometries: points.map(e => ({
position: new TMap.LatLng(e.lat, e.lng),
2022-07-02 15:19:27 +08:00
id: e.id,
2022-07-02 11:36:13 +08:00
content: `${e.name}`,
properties: {...e}
})) || [],
2022-07-07 10:31:54 +08:00
zoomOnClick: true,
maxZoom: 18
2022-07-01 14:31:08 +08:00
})
2022-07-04 09:21:01 +08:00
this.setCenter(points.map(e => {
return new TMap.LatLng(e.lat, e.lng)
}))
2022-07-02 11:36:13 +08:00
let cls = []
this.MarkerCluster.on('cluster_changed', () => {
if (this.markers.length) {
this.markers.forEach(function (item) {
item.destroy();
2022-07-01 14:31:08 +08:00
})
}
2022-07-02 11:36:13 +08:00
cls.forEach(e => e.destroy())
cls = []
let clusters = this.MarkerCluster.getClusters()
clusters.forEach((item) => {
if (item.geometries?.length > 1) {
//聚合样式
let clusterBubble = new ClusterBubble({
map,
position: item.center,
content: item.geometries.length,
customClass: 'cluster'
})
clusterBubble.on('click', () => {
map.fitBounds(item.bounds);
});
cls.push(clusterBubble)
} else {
//点标记样式
2022-07-02 15:19:27 +08:00
let {content, id} = item.geometries?.[0] || {},
2022-07-02 11:36:13 +08:00
overlay = new ClusterBubble({map, position: item.center, content})
overlay.on('click', () => {
2022-07-02 15:19:27 +08:00
uni.navigateTo({
url: `../AppMerchantManage/detail?id=${id}`
})
2022-07-02 11:36:13 +08:00
})
this.markers.push(overlay)
}
});
2022-07-01 14:31:08 +08:00
})
2022-07-02 11:36:13 +08:00
return Promise.resolve()
2022-07-01 14:31:08 +08:00
} else {
2022-07-02 11:36:13 +08:00
if (count < 5) {
setTimeout(() => {
return this.getMarkerCluster(points, ++count)
}, 1000)
} else Promise.reject("加载失败")
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
},
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
initMap (retryTimes = 0) {
this.$nextTick(() => {
let { map, TMap } = this
if (map) {
map.setZoom(15)
} else {
if (retryTimes < 10) {
setTimeout(() => {
this.initMap(++retryTimes)
}, 500)
}
}
})
2022-07-04 09:21:01 +08:00
},
setCenter (arr) {
var bounds = new TMap.LatLngBounds()
arr.forEach(function(item){
if (bounds.isEmpty() || !bounds.contains(item)){
bounds.extend(item)
}
})
this.map.fitBounds(bounds, {
padding: 100
})
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
}
2022-07-01 14:31:08 +08:00
}
</script>
<style lang="scss" scoped>
2022-07-02 11:36:13 +08:00
.AppMerchantMap {
2022-07-02 18:09:24 +08:00
height: 100vh;
overflow: hidden;
.search-list {
position: fixed;
left: 50%;
top: 128px;
width: 718px;
height: 70%;
overflow-y: scroll;
box-sizing: border-box;
z-index: 2222;
transform: translateX(-50%);
.search-icon {
width: 36px;
height: 36px;
margin-right: 16px;
vertical-align: sub;
}
.title {
height: 82px;
line-height: 82px;
font-size: 26px;
font-family: MicrosoftYaHeiSemibold;
color: #1365DD;
padding-left: 44px;
background-color: #fff;
}
.item {
padding: 20px 30px;
background-color: #fff;
&:last-child {
border: none;
}
}
.item-content {
display: inline-block;
width: 610px;
color: #333;
h3 {
font-size: 28px;
font-family: MicrosoftYaHeiSemibold;
line-height: 32px;
margin-bottom: 8px;
}
p {
width: 100%;
font-size: 24px;
font-family: MicrosoftYaHei;
line-height: 32px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
.user-icon {
vertical-align: top;
}
.border {
border-bottom: 1px solid #DEDFE1;
}
}
2022-07-04 10:09:50 +08:00
@keyframes warn {
0% {
transform: scale(.5);
opacity: 1
}
30% {
opacity: .5
}
to {
transform: scale(1.6);
opacity: 0
}
}
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
::v-deep.cluster {
2022-07-01 14:31:08 +08:00
display: flex;
2022-07-02 11:36:13 +08:00
align-items: center;
justify-content: center;
2022-07-04 10:09:50 +08:00
flex-direction: column;
width: 120px;
height: 120px;
color: #fff;
border-radius: 50%;
background: #0f8f64;
&:after {
position: absolute;
z-index: -1;
width: 120px;
height: 120px;
border-radius: 50%;
-webkit-animation: warn 1s ease-out 0s infinite;
animation: warn 1s ease-out 0s infinite;
background-color: #0f8f64;
content: " ";
}
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
::v-deep.marker {
color: #fff;
background: #558BFE;
padding: 0 32px;
width: fit-content;
height: 56px;
border-radius: 52px;
transform: translate(-50%, -50%);
display: flex;
align-items: center;
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
&:before {
content: " ";
display: block;
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
transform: translate(-50%, 100%);
border: 12px solid transparent;
border-top-color: #558BFE;
2022-07-01 14:31:08 +08:00
}
}
2022-07-02 11:36:13 +08:00
* {
box-sizing: border-box;
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
.search {
display: flex;
position: fixed;
align-items: center;
top: 24px;
left: 50%;
z-index: 11111;
width: 718px;
height: 88px;
padding: 0 16px;
background: #FFFFFF;
box-shadow: 0px 4px 8px 0px rgba(192, 185, 185, 0.5);
border-radius: 16px;
transform: translateX(-50%);
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
image {
width: 48px;
height: 48px;
margin-right: 16px;
2022-07-01 14:31:08 +08:00
}
2022-07-02 11:36:13 +08:00
input {
flex: 1;
font-size: 28px;
color: #666666;
2022-07-01 14:31:08 +08:00
}
}
2022-07-02 11:36:13 +08:00
.map-content {
width: 100%;
height: 100vh;
2022-07-01 14:31:08 +08:00
2022-07-02 11:36:13 +08:00
map {
width: 100%;
height: 100vh;
}
}
2022-07-01 14:31:08 +08:00
}
</style>