vue3使用腾讯地图选择地点

news/2024/7/10 2:23:24 标签: vue
<template>
    <div>
        <el-dialog width="60%" :before-close="cancel" v-model="props.visible">
            <span>
                <el-autocomplete v-model="addressKeyword" placeholder="请输入地址,例如临沂人民广场" clearable
                    style="margin-bottom:20px;width:100%;" :fetch-suggestions="querySearch" @select="handleSelect">
                    <template #append>
                        <el-button icon="el-icon-search" @click="getAddressKeyword"></el-button>
                    </template>
                </el-autocomplete>
                <div id="container" style="width:100%;height:600px;"></div>
            </span>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
  
<script setup>
import { ref, reactive, onMounted, nextTick } from "vue";
import md5 from 'js-md5';
import { jsonp } from 'vue-jsonp'
var markerLayer

const markersArray = ref([]);
const restaurants = ref([]);
const map = ref(null);
const getAddress = ref(null);
const getAddCode = ref(null);
const addressKeyword = ref('');
const shopInfo = reactive({
    lng: '',
    lat: '',
    address: ''
});

const props = defineProps({
    visible: Boolean
})

onMounted(() => {
    nextTick(() => {
        init()
    })
})
// 初始化地图
const init = () => {
    // 定义地图中心点坐标
    var center = new window.TMap.LatLng(35.05151, 118.34787)
    // 定义map变量,调用 TMap.Map() 构造函数创建地图
    map.value = new window.TMap.Map(document.getElementById('container'), {
        center: center, // 设置地图中心点坐标
        zoom: 13, // 设置地图缩放级别
        pitch: 0, // 设置俯仰角
        rotation: 0 // 设置地图旋转角度
    })
    // 获取点击后的地址
    map.value.on('click', function (event) {
        shopInfo.address = event.poi.name
        // 获取点击后的地图坐标并设置Marker
        shopInfo.lng = event.latLng.lng
        shopInfo.lat = event.latLng.lat
        if (markerLayer) {
            markerLayer.setGeometries([])
        }
        markerLayer = new window.TMap.MultiMarker({
            map: map.value,
            styles: {
                // 点标记样式
                marker: new window.TMap.MarkerStyle({
                    width: 20, // 样式宽
                    height: 30, // 样式高
                    anchor: { x: 10, y: 30 }, // 描点位置
                    src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                })
            }
        })
        // markerLayer.add({ position: event.latLng })
        markerLayer.add({
            id: shopInfo.lat,
            position: new window.TMap.LatLng(shopInfo.lat, shopInfo.lng)
        })
    })
    if (lat && lng) {
        if (markerLayer) {
            markerLayer.setGeometries([])
        }
        markerLayer = new window.TMap.MultiMarker({
            map: map.value,
            styles: {
                // 点标记样式
                marker: new window.TMap.MarkerStyle({
                    width: 20, // 样式宽
                    height: 30, // 样式高
                    anchor: { x: 10, y: 30 }, // 描点位置
                    src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                })
            }
        })
        markerLayer.updateGeometries({
            id: lat,
            position: new window.TMap.LatLng(lat, lng)
        })
        map.value.setCenter(new window.TMap.LatLng(lat, lng))
    }
}
const querySearch = (queryString, cb) => {
    var results = queryString ? restaurants.value.filter(createFilter(queryString)) : restaurants.value
    // 调用 callback 返回建议列表的数据
    cb(results)
}
const createFilter = (queryString) => {
    return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
    }
}
const handleSelect = (item) => {
    // 腾讯地图文档: https://lbs.qq.com/faq/serverFaq/webServiceKey
    // csdn参考:https://blog.csdn.net/tiankonlan/article/details/126363935
    // 申请的key:import.meta.env.VITE_APP_TMapKey     签名:import.meta.env.VITE_APP_TMapSig
    let str = `/ws/geocoder/v1/?address=${item || addressKeyword.value}&callback=jsonpCallback&key=${import.meta.env.VITE_APP_TMapKey}&output=jsonp${import.meta.env.VITE_APP_TMapSig}`
    let sig = md5(str);
    sig = encodeURI(sig)

    let getData = {
        address: item || addressKeyword.value,
        callbackQuery: 'callback',
        callbackName: 'jsonpCallback',
        output: 'jsonp',
        key: import.meta.env.VITE_APP_TMapKey,
        sig: sig
    }

    jsonp('https://apis.map.qq.com/ws/geocoder/v1/', getData).then(response => {
        if (response.message === '查询无结果') {
            alert('查询无结果')
            return false
        }
        shopInfo.lng = response.result.location.lng
        shopInfo.lat = response.result.location.lat
        if (markerLayer) {
            markerLayer.setGeometries([])
        }
        markerLayer = new window.TMap.MultiMarker({
            map: map.value,
            styles: {
                // 点标记样式
                marker: new window.TMap.MarkerStyle({
                    width: 20, // 样式宽
                    height: 30, // 样式高
                    anchor: { x: 10, y: 30 }, // 描点位置
                    src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                })
            }
        })
        map.value.setCenter(new window.TMap.LatLng(shopInfo.lat, shopInfo.lng))
        markerLayer.updateGeometries({
            id: shopInfo.lat,
            position: new window.TMap.LatLng(shopInfo.lat, shopInfo.lng)
        })
    }).catch(function (error) {
        console.log(error)
    })
}
const getAddressKeyword = () => {
    handleSelect(addressKeyword.value)
}
const emit = defineEmits(['map-confirm', 'cancel'])
/***
 * 确认
 */
const confirm = () => {
    let data = JSON.parse(JSON.stringify(shopInfo))
    emit('map-confirm', data)
}
/***
 * 取消
 */
const cancel = () => {
    emit('cancel')
}
</script>
  
<style lang="scss" scoped>
.serachinput {
    width: 300px;
    box-sizing: border-box;
    padding: 9px;
    border: 1px solid #dddee1;
    line-height: 20px;
    font-size: 16px;
    height: 38px;
    color: #333;
    position: relative;
    border-radius: 4px;
    -webkit-box-shadow: #666 0px 0px 10px;
    -moz-box-shadow: #666 0px 0px 10px;
    box-shadow: #666 0px 0px 10px;
}

:deep(.el-dialog__header) {
    border-bottom: 0 !important;
}
</style>
  
  

http://www.niftyadmin.cn/n/5081562.html

相关文章

相同的 key, Nacos 配置一定会覆盖 application.yaml 吗?

一&#xff0c;背景&#xff1a; 之前在使用 Nacos 的时候有个疑问&#xff0c;同样的 key&#xff0c;在 Nacos 配置了&#xff0c;在 application 也配置了&#xff0c;到底会有哪个呢&#xff1f;一直没空查&#xff0c;后来在网上看到过文章&#xff0c;说 Nacos 是在 app…

Kotlin的作用域函数 let、also、with、run、apply

作用域函数主要有下面这几种&#xff0c;apply &#xff0c;with 、run 、let 、以及 also 。这些函数非常类似&#xff0c;它们的主要区别&#xff1a; 引⽤上下⽂对象的⽅式 &#xff08;this / it&#xff09;返回值 他们在开发中的使用场景主要有两个&#xff0c;一是非空…

2023全国大学生软件测试大赛开发者测试练习题满分答案(PairingHeap2023)

2023全国大学生软件测试大赛开发者测试练习题满分答案&#xff08;PairingHeap2023&#xff09; 题目详情题解代码&#xff08;直接全部复制到test类中即可&#xff09; 提示&#xff1a;该题只需要分支覆盖得分即可&#xff0c;不需要变异得分 题目详情 题解代码&#xff08;…

Python之函数详解

一、函数的定义与调用 函数定义语法&#xff1a; def 函数名([参数列表]): ‘’‘注释’‘’ 函数体 注意事项 函数形参不需要声明类型&#xff0c;也不需要指定函数返回值类型即使该函数不需要接收任何参数&#xff0c;也必须保留一对空的圆括号 括号后面的冒号必不可少函数…

常见Vue事件修饰符浅析

一、.stop修饰符 .stop修饰符代表event.stopPropagation()&#xff0c;加上这个修饰符&#xff0c;就等于在方法中加上了这句代码。 <!--阻止单击事件继续传播--> <a click.stop"doThis"></a>上面的代码等同于如下代码。 <!--阻止单击事件继…

第十一章-用户进程

Ⅰ.为什么需要任务状态段TSS 1.LDT简介 已经不使用LDT加载任务了。因为当前运行的任务&#xff0c;其 LDT 位于 LDTR 指向的地址&#xff0c;这样 CPU 才能从中拿到任务运行所需要的资源&#xff08;指令和数据&#xff09;。因此&#xff0c;每切换一个任务时&#xff0c;需…

图像特征算法---ORB算法的python实现

一、ORB算法 1.算法简介 ORB 是 Oriented Fast and Rotated Brief 的简称&#xff0c;可以用来对图像中的关键点快速创建特征向量&#xff0c;这些特征向量可以用来识别图像中的对象。 其中&#xff0c;Fast 和 Brief 分别是特征检测算法和向量创建算法。ORB 首先会从图像中…