| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <template>
- <el-select class="location-search" v-model="locationVal" filterable remote clearable reserve-keyword :placeholder="$t('搜索位置')"
- :remote-method="remoteMethod" :loading="loading" @change="handleSearchRes" popper-class="location-search-popper"
- :class="customClass" @clear="handleClear">
- <el-option v-for="(item, index) in locationOptions.list" :key="index" :label="item.title"
- :value="{ value: item.point, item }">
- <span>{{ item.title }}</span>
- <span class="sub-title">{{ item.province }}{{ item.city }}{{ item.district }}</span>
- </el-option>
- <template #prefix>
- <el-icon><Search /></el-icon>
- </template>
- </el-select>
- </template>
- <script setup>
- import { ref, reactive } from "vue";
- import { transformFromGCJToWGS } from "@/utils/WSCoordinate.js";
- const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
- const props = defineProps({
- userLocation: {
- type: String,
- default: "113.61702297075017,23.584863449735067",
- },
- customClass: {
- type: String,
- default: "",
- },
- });
- const emit = defineEmits(["change"]);
- const locationVal = ref(null);
- const locationOptions = reactive({
- list: [],
- });
- const loading = ref(false);
- const remoteMethod = async (keyword) => {
- if (keyword) {
- locationOptions.list = [];
- loading.value = true;
- const params = {
- key: MAP_KEY,
- keyword,
- location: props.userLocation,
- };
- await VE_API.old_mini_map.getCtiyList({ word: keyword }).then(({ data }) => {
- if (data && data.length) {
- data.forEach((item) => {
- item.point = item.location.lat + "," + item.location.lng;
- locationOptions.list.push(item);
- });
- }
- });
- VE_API.old_mini_map.search(params).then(({ data }) => {
- loading.value = false;
- data.forEach((item) => {
- item.point = item.location.lat + "," + item.location.lng;
- locationOptions.list.push(item);
- });
- });
- } else {
- locationOptions.list = [];
- }
- };
- const handleSearchRes = (v) => {
- if (!v) return;
- const parts = v.value.split(",");
- let { latitude, longitude } = transformFromGCJToWGS(parseFloat(parts[0]), parseFloat(parts[1]));
- const coordinateArray = [longitude, latitude];
- emit("change", {
- coordinateArray,
- point: `POINT (${coordinateArray[0]} ${coordinateArray[1]})`,
- address: v.item?.title || v.item?.address,
- pointAddress: v.item?.province + v.item?.city + v.item?.district,
- city: v.item?.city + v.item?.district || '',
- item: v.item,
- });
- };
- const handleClear = () => {
- emit("change", {
- coordinateArray: null,
- point: null,
- address: null,
- });
- locationVal.value = null;
- locationOptions.list = [];
- loading.value = false;
- };
- </script>
- <style lang="scss">
- .location-search {
- .el-select__wrapper {
- background: rgba(0, 0, 0, 0.3);
- box-shadow: none;
- border-radius: 25px;
- border: 1px solid rgba(255, 255, 255, 0.4);
- }
- .el-select__placeholder{
- color: rgba(255, 255, 255, 0.7);
- }
- .el-select__input {
- color: rgba(255, 255, 255, 0.7);
- }
- }
- .location-search-popper {
- .el-select-dropdown__list {
- max-width: 96vw;
- overflow-x: auto;
- }
- .sub-title {
- padding-left: 6px;
- font-size: 12px;
- color: #ccc;
- }
- }
- </style>
|