Files
cmanager/src/components/map/selectLocation.vue

220 lines
5.7 KiB
Vue
Raw Normal View History

2024-02-02 15:04:47 +08:00
<template>
<div class="app-container">
<el-autocomplete
v-model="addressLocation"
style="width:100%;"
popper-class="autoAddressClass"
:fetch-suggestions="querySearchAsync"
:trigger-on-focus="false"
placeholder="详细地址"
clearable="false"
@select="handleSelect"
:disabled="!isCanEdit"
2024-02-04 22:34:37 +08:00
v-show="type !== 'view'"
2024-02-02 15:04:47 +08:00
@input="addressChangeHandle"
>
<template slot-scope="{ item }">
<div style="overflow:hidden;">
<div class="title">{{ item.title }}</div>
<span class="address ellipsis">{{ item.address }}</span>
</div>
</template>
</el-autocomplete>
<div ref="mapContainer" style="width:100%;height:300px;" v-if="isCanEdit" />
<div ref="viewMap" style="width:100%;height:300px;" v-else />
</div>
</template>
<script>
/* eslint-disable */
import { getcoder, querySearch } from "@/api/tenant/map";
export default {
props: {
lat: {
type: "string",
2024-02-04 22:34:37 +08:00
default: 23.1315381
2024-02-02 15:04:47 +08:00
},
lng: {
type: "string",
2024-02-04 22:34:37 +08:00
default: 113.3324436
2024-02-02 15:04:47 +08:00
},
address: String,
isCanEdit: Boolean,
type: {
type: "string",
2024-02-04 22:34:37 +08:00
default: ""
}
2024-02-02 15:04:47 +08:00
},
2024-02-04 22:34:37 +08:00
data() {
2024-02-02 15:04:47 +08:00
return {
map: "", // 地图实例
mk: "", // Marker实例
map2: "", // 地图实例
mk2: "", // Marker实例,
addressLocation: this.address,
2024-02-04 22:34:37 +08:00
key: "MTCBZ-WAAWC-H3M22-AL6XI-G4XN6-YZFK5",
addressTip: []
2024-02-02 15:04:47 +08:00
};
},
2024-02-04 22:34:37 +08:00
mounted() {
2024-02-02 15:04:47 +08:00
this.initMap();
},
watch: {},
methods: {
2024-02-04 22:34:37 +08:00
addressChangeHandle(val) {
if (val == "") {
this.$emit("addressDel", "删除了地址数据");
2024-02-02 15:04:47 +08:00
}
},
// 初始化地图
2024-02-04 22:34:37 +08:00
initMap() {
2024-02-02 15:04:47 +08:00
var that = this;
if (this.isCanEdit) {
var point = new qq.maps.LatLng(this.lat, this.lng);
// 1、挂载第一张地图
this.map = new qq.maps.Map(this.$refs.mapContainer, {
center: point,
zoom: 19,
//启用缩放控件
zoomControl: true,
//地图缩放控件参数
zoomControlOptions: {
2024-02-04 22:34:37 +08:00
position: qq.maps.ControlPosition.TOP_LEFT
2024-02-02 15:04:47 +08:00
},
//地图比例尺控件若为false则不显示比例尺控件
2024-02-04 22:34:37 +08:00
scaleControl: false
2024-02-02 15:04:47 +08:00
});
// 3、设置图像标注并绑定拖拽标注结束后事件
this.mk = new qq.maps.Marker({
position: point,
draggable: true,
2024-02-04 22:34:37 +08:00
map: that.map
2024-02-02 15:04:47 +08:00
});
// 绑定拖拽标注结束后事件
2024-02-04 22:34:37 +08:00
qq.maps.event.addListener(this.mk, "dragend", function(e) {
2024-02-02 15:04:47 +08:00
that.getAddrByPoint(e.latLng);
});
//6、浏览器定位
if (this.type === "add") {
this.geolocation();
}
// 7、绑定点击地图任意点事件
2024-02-04 22:34:37 +08:00
qq.maps.event.addListener(this.map, "click", function(e) {
2024-02-02 15:04:47 +08:00
that.getAddrByPoint(e.latLng);
});
} else {
var point2 = new qq.maps.LatLng(this.lat, this.lng);
// 1、挂载第二张地图
this.map2 = new qq.maps.Map(this.$refs.viewMap, {
center: point2,
zoom: 19,
//地图缩放控件参数
zoomControlOptions: {
2024-02-04 22:34:37 +08:00
position: qq.maps.ControlPosition.TOP_LEFT
2024-02-02 15:04:47 +08:00
},
//地图比例尺控件若为false则不显示比例尺控件
2024-02-04 22:34:37 +08:00
scaleControl: false
2024-02-02 15:04:47 +08:00
});
// 2、设置图像标注并绑定拖拽标注结束后事件
this.mk2 = new qq.maps.Marker({
position: point2,
draggable: false,
2024-02-04 22:34:37 +08:00
map: that.map2
2024-02-02 15:04:47 +08:00
});
}
},
// 浏览器定位函数
2024-02-04 22:34:37 +08:00
geolocation() {
2024-02-02 15:04:47 +08:00
var that = this;
var geolocation = new qq.maps.Geolocation(
2024-02-04 22:34:37 +08:00
"MTCBZ-WAAWC-H3M22-AL6XI-G4XN6-YZFK5",
"后台管理"
2024-02-02 15:04:47 +08:00
);
geolocation.getLocation(this.showPosition, this.showErr, {
timeout: 20000,
2024-02-04 22:34:37 +08:00
failTipFlag: true
2024-02-02 15:04:47 +08:00
});
},
2024-02-04 22:34:37 +08:00
showPosition(position) {
2024-02-02 15:04:47 +08:00
this.getAddrByPoint({
lat: position.lat,
2024-02-04 22:34:37 +08:00
lng: position.lng
2024-02-02 15:04:47 +08:00
});
},
2024-02-04 22:34:37 +08:00
showErr() {
2024-02-02 15:04:47 +08:00
console.log("定位失败!");
},
// // 2、逆地址解析函数
2024-02-04 22:34:37 +08:00
getAddrByPoint(point) {
2024-02-02 15:04:47 +08:00
var currentPoint = new qq.maps.LatLng(point.lat, point.lng);
var that = this;
var location = point.lat + "," + point.lng;
2024-02-04 22:34:37 +08:00
getcoder(location, encodeURI(this.key), 0).then(res => {
2024-02-02 15:04:47 +08:00
that.mk.setPosition(currentPoint);
that.map.panTo(currentPoint);
let obj = {
address: res.data.result.address,
lng: point.lng,
2024-02-04 22:34:37 +08:00
lat: point.lat
2024-02-02 15:04:47 +08:00
};
2024-02-04 22:34:37 +08:00
that.addressLocation = obj.address;
2024-02-02 15:04:47 +08:00
that.$emit("addAddress", obj);
});
},
2024-02-04 22:34:37 +08:00
async querySearchAsync(queryString, cb) {
await querySearch(queryString, encodeURI(this.key)).then(res => {
2024-02-02 15:04:47 +08:00
if (res.data.status === 0) {
this.addressTip = res.data.data;
}
});
var results = this.addressTip;
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 1000 * Math.random());
},
// 8-2、选择地址
2024-02-04 22:34:37 +08:00
handleSelect(item) {
2024-02-02 15:04:47 +08:00
this.addressLocation = item.address;
var point = new qq.maps.LatLng(item.location.lat, item.location.lng);
this.mk.setPosition(point);
this.map.panTo(point);
let obj = {
title: item.title,
address: item.address,
lng: item.location.lng,
2024-02-04 22:34:37 +08:00
lat: item.location.lat
2024-02-02 15:04:47 +08:00
};
this.$emit("addAddress", obj);
2024-02-04 22:34:37 +08:00
}
}
2024-02-02 15:04:47 +08:00
};
</script>
<style lang="scss" scoped>
.autoAddressClass {
li {
i.el-icon-search {
margin-top: 11px;
}
.mgr10 {
margin-right: 10px;
}
.title {
text-overflow: ellipsis;
overflow: hidden;
}
.address {
line-height: 1;
font-size: 12px;
color: #b4b4b4;
margin-bottom: 5px;
}
}
}
</style>