news 2026/5/7 20:09:38

高德地图做选点,根据选点调用逆地理编码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
高德地图做选点,根据选点调用逆地理编码

记录一下,大部分也是ai写的
注意的是,逆地理编码需要用的key是web服务端的,所以得重新申请一个key
意外的是,我在官方的文档里面运行,用我自己的key,不好使。(官方直接调用逆地理编码的那个方法就可以),这个没好使。
但又找到了一个调用地址,调用这个地址用自己的key好使,我没理解为什么。https://restapi.amap.com/v3/geocode/regeo?output=json&location=${lng},${lat}&key=${amapKey}&radius=1000&extensions=all

<template> <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible" append-to-body title="扫码地图" width="1000px" > <!-- 地图容器 --> <el-amap ref="map" :center="center" :zoom="zoom" class="amap-demo" vid="amapDemo" @click="handleMapClick" @init="handleMapInit" > <!-- 点击位置标记(修复图标配置) --> <el-amap-marker v-if="selectedPosition.lng && selectedPosition.lat" :position="[selectedPosition.lng, selectedPosition.lat]" :draggable="true" @dragend="handleMarkerDragEnd" ></el-amap-marker> </el-amap> <!-- 底部位置信息 --> <div class="position-info"> <span>所选位置:{{ selectedPosition.address || '请点击地图选择位置' }}</span> </div> <!-- 底部按钮 --> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取消</el-button> <el-button :disabled="!selectedPosition.lng" type="primary" @click="handleConfirm">确定</el-button> </div> </el-dialog> </template> <script> export default { name: 'SelectMap', data() { return { zoom: 5, // 放大默认层级 center: [104.937478, 35.439575], // 初始中心点 dialogVisible: false, selectedPosition: { lng: '', lat: '', address: '' }, geocoder: null, // 逆地理编码实例 mapInstance: null, // 地图实例 markerInstance: null // 原生Marker实例(备用方案) } }, methods: { /** * 地图初始化完成 */ handleMapInit(map) { this.mapInstance = map; // 初始化逆地理编码(修复城市编码,设为空表示全国) this.geocoder = new AMap.Geocoder({ city: "", // 改为空,避免城市限制导致解析失败 radius: 1000, extensions: 'all' // 增加扩展参数,获取更全地址信息 }); // 自适应地图大小(解决弹窗初始化地图显示问题) setTimeout(() => { map.resize(); }, 100); }, /** * 点击地图事件(修复核心逻辑) */ handleMapClick(e) { try { const { lng, lat } = e.lnglat; // 更新选中位置的经纬度 this.selectedPosition.lng = lng; this.selectedPosition.lat = lat; // 更新地图中心点 this.center = [lng, lat]; // 逆地理编码获取地址 this.getAddressByLnglat(lng, lat); // 【可选】如果vue-amap的marker仍有问题,改用原生Marker // this.createNativeMarker(lng, lat); } catch (error) { console.error('地图点击事件异常:', error); } }, /** * 标记拖动结束事件 */ handleMarkerDragEnd(e) { const { lng, lat } = e.lnglat; this.selectedPosition.lng = lng; this.selectedPosition.lat = lat; this.getAddressByLnglat(lng, lat); }, /** * 逆地理编码:根据经纬度获取地址(修复权限错误) */ async getAddressByLnglat(lng, lat) { const amapKey = this.$mapConfig.gaodeWebServiceKey const url = `https://restapi.amap.com/v3/geocode/regeo?output=json&location=${lng},${lat}&key=${amapKey}&radius=1000&extensions=all`; try { // 注意:需开启跨域(浏览器端需配置Referer白名单) const res = await fetch(url, { method: 'GET', mode: 'cors' // 跨域模式 }); const data = await res.json(); if (data.status === '1' && data.regeocode) { this.selectedPosition.address = data.regeocode.formatted_address; } else { this.selectedPosition.address = `解析失败:${data.info || data.result}`; console.error('高德API返回错误:', data); } } catch (error) { this.selectedPosition.address = `经纬度:${lng.toFixed(6)}, ${lat.toFixed(6)}`; console.error('网络/跨域错误:', error); } }, /** * 【备用方案】创建原生Marker(避免vue-amap兼容问题) */ createNativeMarker(lng, lat) { // 先移除旧标记 if (this.markerInstance) { this.mapInstance.remove(this.markerInstance); } // 创建新标记(使用高德默认图标) this.markerInstance = new AMap.Marker({ position: [lng, lat], map: this.mapInstance, draggable: true }); // 监听原生标记拖动 this.markerInstance.on('dragend', (e) => { const { lng, lat } = e.lnglat; this.selectedPosition.lng = lng; this.selectedPosition.lat = lat; this.getAddressByLnglat(lng, lat); }); }, /** * 打开地图选择弹窗 */ open(initPosition = {}) { this.dialogVisible = true; // 重置选中位置 this.selectedPosition = { lng: '', lat: '', address: '' }; // 如果有初始定位,设置中心点和选中位置 if (initPosition.lng && initPosition.lat) { this.center = [initPosition.lng, initPosition.lat]; this.selectedPosition.lng = initPosition.lng; this.selectedPosition.lat = initPosition.lat; this.getAddressByLnglat(initPosition.lng, initPosition.lat); } else { // 默认显示全国中心 this.center = [104.937478, 35.439575]; } // 延迟确保地图渲染完成 setTimeout(() => { if (this.mapInstance) { this.mapInstance.resize(); } }, 200); }, /** * 确认选择位置 */ handleConfirm() { this.$emit('confirm', { lng: this.selectedPosition.lng, lat: this.selectedPosition.lat, address: this.selectedPosition.address }); this.dialogVisible = false; } }, beforeDestroy() { // 清理地图实例和标记 if (this.markerInstance) { this.mapInstance.remove(this.markerInstance); } if (this.mapInstance) { this.mapInstance.destroy(); } } } </script> <style lang="scss" scoped> .amap-demo { height: 600px; width: 100%; } .position-info { padding: 12px 15px; background-color: #f5f7fa; border-radius: 4px; margin-top: 10px; span { font-size: 14px; color: #606266; line-height: 1.5; } } .dialog-footer { text-align: right; } </style>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/3 9:24:15

软件测试专栏——黑盒测试

黑盒测试概述黑盒测试&#xff0c;它是通过测试来检测每个功能是否都能正常使用。在测试中&#xff0c;把程序看作一个不能打开的黑盒子&#xff0c;在完全不考虑程序内部结构和内部特性的情况下&#xff0c;在程序接口进行测试&#xff0c;它只检查程序功能是否按照需求规格说…

作者头像 李华
网站建设 2026/5/4 22:17:37

从零搭建量子ML可视化环境,手把手教你配置VSCode的6个关键插件

第一章&#xff1a;量子机器学习的 VSCode 数据可视化在量子机器学习研究中&#xff0c;数据可视化是理解高维量子态与模型行为的关键环节。借助 Visual Studio Code&#xff08;VSCode&#xff09;强大的扩展生态&#xff0c;开发者能够将复杂的量子计算结果以直观图形呈现&am…

作者头像 李华
网站建设 2026/5/3 5:54:33

Azure CLI导出量子计算结果的隐藏命令,资深工程师不愿透露的秘密

第一章&#xff1a;Azure CLI量子作业结果导出概述在使用 Azure Quantum 服务进行量子计算实验时&#xff0c;用户通常需要将作业执行结果导出至本地或第三方系统进行后续分析。Azure CLI 提供了一套简洁高效的命令行接口&#xff0c;支持用户查询、获取并导出量子作业的运行结…

作者头像 李华
网站建设 2026/4/30 23:13:43

从零构建边缘Docker监控体系(含Prometheus+Grafana实战配置)

第一章&#xff1a;边缘Docker监控的挑战与架构设计 在边缘计算环境中&#xff0c;Docker容器广泛用于部署轻量级、可移植的应用服务。然而&#xff0c;受限的硬件资源、网络不稳定性和地理分布特性&#xff0c;给监控系统的构建带来了显著挑战。传统的集中式监控方案难以适应边…

作者头像 李华
网站建设 2026/5/1 19:26:25

5个关键技巧:完全掌握DuckDB与C++嵌入式数据库集成

5个关键技巧&#xff1a;完全掌握DuckDB与C嵌入式数据库集成 【免费下载链接】duckdb 项目地址: https://gitcode.com/gh_mirrors/duc/duckdb 还在为C应用寻找合适的数据存储方案而烦恼吗&#xff1f;想要在应用中嵌入高性能分析能力却担心复杂的依赖关系&#xff1f;D…

作者头像 李华
网站建设 2026/5/6 23:24:08

macOS窗口管理终极指南:开源工具完全手册

macOS窗口管理终极指南&#xff1a;开源工具完全手册 【免费下载链接】open-source-mac-os-apps serhii-londar/open-source-mac-os-apps: 是一个收集了众多开源 macOS 应用程序的仓库&#xff0c;这些应用程序涉及到各种领域&#xff0c;例如编程、生产力工具、游戏等。对于开…

作者头像 李华