当前位置: 首页 > ds >正文

前端-高德地图选点带搜索功能

先看效果
在这里插入图片描述
地图组件

<template><el-dialogtitle="选择位置"width="800px":visible.sync="showDialog"><div class="mapContainer"><div class="mapSearchBox" v-loading="loading"><div class="mapSearch" v-show="!loading"><el-input placeholder="请输入你要查找的关键词" class="tishikuang" v-model="userInput" id="tishikuang"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input></div><div id="container"></div><div style="text-align: right;margin-bottom: 10px;margin-top: 20px;"><el-button @click="destroyMap">取消</el-button><el-button type="primary" @click="biu">确定</el-button></div></div></div></el-dialog>
</template><script>import AMapLoader from '@amap/amap-jsapi-loader'window._AMapSecurityConfig = {securityJsCode: '你的秘钥'//安全密钥}export default {props: ['zIndex', 'showDialog'],data() {return {visible:true,loading: false,map: null,autoOptions: {input: 'tishikuang' //绑定的搜索关键字的input标签ID},auto: null,userInput: '', //绑定的搜索关键字的的内容placeSearch: null,searchHere: null, //根据搜索组件搜索到以后的地点信息marker: null, //地图标点lnglat: null, //点击地图以后的经纬度geocoder: null,address: ''}},mounted() {setTimeout(() => {this.initMap()}, 500)},methods: {// poi搜索以后 点击确认选址以后的操作biu() {if (this.lnglat) {console.log('选择地点:' + this.lnglat);this.$emit('select', this.lnglat,this.address);} else {this.$message({message: '请选择位置',type: 'warning'});}},initMap() {this.loading = true;// 加载地图AMapLoader.load({key: "你申请的key",version: "2.0",plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.MapType','AMap.Geolocation', 'AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geocoder'],}).then((AMap) => {this.map = new AMap.Map("container", { //设置地图容器idviewMode: "3D", //是否为3D地图模式zoom: 17, //初始化地图级别// center: [105.602725, 37.076636], //初始化地图中心点位置});this.auto = new AMap.AutoComplete(this.autoOptions)//this.placeSearch = new AMap.PlaceSearch({map: this.map}) //构造地点查询类this.auto.on('select', this.select)//绑定查询事件this.marker = new AMap.Marker({zIndex:999});this.geocoder = new AMap.Geocoder({// city: "010", //城市设为北京,默认:“全国”// radius: 1000 //范围,默认:500})// 添加点击事件this.map.on('click', (e) => {console.log('点击位置:', e.lnglat);this.map.add(this.marker);this.marker.setPosition(e.lnglat);this.lnglat = e.lnglat;this.geocoder.getAddress(this.lnglat, (status, result) => {if (status === 'complete' && result.regeocode) {this.address = result.regeocode.formattedAddressconsole.log(',,,,'+this.address)}})});}).catch(e => {console.log(e);}).finally(() => {this.loading = false;});},//poi搜索 这里面的这个参数 e 就是搜索以后的地址信息 你可以把它打印出来看一看经纬度什么的都有select(e) {console.log('poi搜索',e)this.placeSearch.setCity(e.poi.adcode) //依据城市ID搜索this.placeSearch.search(e.poi.name) //关键字查询查询this.searchHere = e.poithis.map.setZoom(10,true)},destroyMap() {if (this.map) {this.map.destroy();this.map = null;}this.$emit('closed')}},}
</script>
<style type="text/css">.amap-logo{display: none;opacity:0 !important;}.amap-copyright {opacity:0;}</style>
<style lang="scss" scoped>#container {padding: 0px;margin: 0px;width: 100%;height: 500px;}.mapSearch {margin-bottom: 8px;width: 300px;margin-top: 20px;margin-right: 30px;.el-button {margin-left: 8px;}}#tishikuang {margin-bottom: 200px;display: block;}.mapContainer{// position: fixed;width: 100%;height: 100%;// top:0;.modal{// width: 100%;// height: 100%;// position: absolute;// top: 0;// left: 0;// background-color: #333;opacity: 0.6;}.mapSearchBox{width: 100%;h3{text-align: left;font-size: 18px;font-weight: 500;}.tishikuang{width: 40%;position: absolute;right: 30px;top: 96px;z-index: 21;}}}</style>

引用组件

<map-dialogv-if="showDialog":showDialog="showDialog"z-index="9999"@select="handleLocationSelect"@closed="()=>{showDialog = false}"></map-dialog>
handleLocationSelect(lnglat,address) {this.showDialog = false // 关闭弹窗console.log('hhhh'+address)console.log('hhhh'+lnglat.lng)console.log('hhhh'+lnglat.lat)},
http://www.xdnf.cn/news/5867.html

相关文章:

  • hashicorp raft源码分析(一、项目介绍与Leder选举实现)
  • 猫咪如厕检测与分类识别系统系列~进阶【三】网页端算法启动架构及数据库实现
  • YOLOv5推理代码解析
  • 医院陪护服务革新:透明化监护与即时救济机制构建
  • 如何优雅的使用CMake中的FindPkgConfig模块
  • 验证回文串1
  • Linux sysvinit 系统启动
  • 【三】 空域滤波的基础与空域中的低通滤波器(2)【830数字图像处理】
  • 解构赋值
  • RuoYi 中使用 PageUtils.startPage() 实现分页查询的完整解析
  • 数字ic后端设计从入门到精通4(含fusion compiler, tcl教学)CMOS VLSI Design
  • Baumer工业相机堡盟工业相机的工业视觉是否可以在室外可以做视觉检测项目
  • 【系统架构师】2025论文《基于架构的软件设计方法》【最新】
  • telnetlib源码深入解析
  • Java面试终极篇:Sentinel+Seata+Kafka Streams高并发架构实战
  • Adobe Acrobat pro在一份PDF中插入空白页
  • 【基于ALS模型的教育视频推荐系统(Java实现)】
  • java反序列化commons-collections链6
  • 邮件营销应对高退信率的策略
  • 一键解锁嵌入式UI开发——LVGL的“万能配方”
  • AI驱动网络范式革新:Smart Switch与智能路由的协同进化
  • 《飞飞重逢》手游:暴力治疗与团队赋能的战场艺术!
  • feign.RequestInterceptor 简介-笔记
  • 深入浅出:Java 中的动态类加载与编译技术
  • 2025.5.12 APIO 模拟赛总结
  • 小结: Port Security,DHCP Snooping,IPSG,DAI,
  • python opencv 将不同shape尺寸的图片制作video视频
  • 法国蒙彼利埃大学团队:运用元动力学模拟与马尔可夫状态模型解锁 G 蛋白偶联受体构象动态机制
  • Linux 服务器用 SSH 拉取多个 Git 工程
  • LeRobot 项目部署运行逻辑(七)—— ACT 在 Mobile ALOHA 训练与部署