1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > vue3 h5腾讯地图组件实现地图选点功能

vue3 h5腾讯地图组件实现地图选点功能

时间:2024-02-09 17:58:49

相关推荐

vue3 h5腾讯地图组件实现地图选点功能

去腾讯地图申请账号,拿到自己的key

直接上完成版代码

<template><div id="app"><el-button @click="openMapPickPop">选点</el-button><div><b>经度:</b><b>{{currentPicked?.latlng.lat }}</b></div><div><b>纬度:</b><b>{{currentPicked?.latlng.lng }}</b></div><div><b>城市:</b><b>{{currentPicked?.cityname }}</b></div><div><b>位置信息:</b><b>{{currentPicked?.poiaddress }}--{{currentPicked?.poiname }}</b></div></div></template><script setup>import {ref, reactive, computed, watch, onMounted } from "vue";import {ElMessageBox } from "element-plus";// const key= "去腾讯地图申请账号,拿到自己的key";const IFRAME_HTML = `<iframe id="mapPage" width="375" height="400" frameborder="0"src="https://apis./tools/locpicker?search=1&type=1&key=${= key}&referer=myapp"></iframe>`;const currentPicked = ref({latlng: {},});const onLocationVal = ref("");function openMapPickPop($event) {if ($event.relatedTarget) return;ElMessageBox({title: "选择地址",message: IFRAME_HTML,customClass: "location-picker-box",dangerouslyUseHTMLString: true,closeOnPressEscape: false,}).then(() => {window.removeEventListener("message", handleMapChoice, false);console.log("选择地图", currentPicked.value);}).catch((e) => {if (e != "cancel") throw e;window.removeEventListener("message", handleMapChoice, false);});window.addEventListener("message", handleMapChoice, false);}// 地图选择的回调function handleMapChoice(event) {let mapData = event.data;// 防止其他应用也会向该页面post信息,需判断module是否为'locationPicker'if (mapData && mapData.module != "locationPicker") return;console.log(mapData);currentPicked.value = mapData;onLocationVal.value = mapData.poiaddress;}</script><style lang="less">html .location-picker-box {width: auto;max-width: none;}iframe {width: 100%;padding: 0;margin: 0;}</style>

参考此文

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。