vue element-ui web端 引入高德地图,并获取经纬度

news/2024/4/30 2:30:39/文章来源:https://blog.csdn.net/sinat_41200024/article/details/130274875

发版前接到一个临时新需求 ,需要在web端地址选择时用地图,并获取经纬度。
临阵发版之际加需求,真的是很头疼,于是赶紧找度娘,找api。
我引入的是高德地图,首先要去申请key , 和密钥,

首先用npm 安装loader

npm i @amap/amap-jsapi-loader --save

然后在main.js里引入

在这里插入图片描述
这里要注意,还需要在index.html文件里引入这一段,开始我没有引入这段,后面请求高德接口时就会报错
在这里插入图片描述
这里我写了一个组件,后面直接引用就可以
组件内容如下:
在这里插入图片描述
内容有点多,截不完图,下面附上源码:

<template lang="html"><el-dialog v-dialogDrag title="选择地点" append-to-body width="600px" :visible.sync="mvisible" :close-on-click-modal="false"@close="cancel"><div id="amap-container"><el-inputid="search-input"v-model="searchValue"class="input-with"placeholder="请输入地址"clearable@clear="handelclearInput"@keyup.native.enter="onhandelSearch"><el-buttonslot="append"size="small"type="primary"icon="el-icon-search"@click="onhandelSearch">搜索</el-button></el-input><el-row class="margin-top-10 address">当前地址是: {{ formattedAddress }}</el-row><div id="custom-amap" /></div><div slot="footer" class="dialog-footer"><el-button type="primary" @click="handelSave">保 存</el-button><el-button @click="cancel">取 消</el-button></div></el-dialog>
</template><script>
import AMapLoader from '@amap/amap-jsapi-loader'
// const AMap = window.AMap
export default {name: 'AMap',props: {defaultValue: {type: String,default: ''},visible: {type: Boolean,default: false}},data() {return {mvisible: false,defaultCity: '',// 地图对象map: null,// 定位默认地址 | 搜索后选择的地址formattedAddress: null,// 地址对应的经纬度信息position: {},// 检索关键字searchValue: '',// 检索函数对象placeSearch: null,// 检索结果数据数据searchInfoList: [],// 地图标记marker: '',// 地址解析(正向)geocoder: '',// 地址名称name: '',adcode: ''}},watch: {defaultValue() {this.searchValue = this.defaultValue},visible() {this.mvisible = this.visiblethis.searchValue = this.defaultValue// this.searchValue = '四川省成都市武侯区'this.formattedAddress = this.defaultValue// 初始化地图页面this.initMap()}},beforeDestroy() {// 销毁地图this.map.destroy()},methods: {//   初始化地图页面initMap() {AMapLoader.load({key: 'dc4da34d26ef0a0851ce91ce099f6f46', // 申请好的Web端开发者Key,首次调用 load 时必填version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15plugins: [''] // 需要使用的的插件列表,如比例尺'AMap.Scale'等}).then((AMap) => {this.map = new AMap.Map('custom-amap', { // 设置地图容器idviewMode: '3D', // 是否为3D地图模式zoom: 5, // 初始化地图级别resizeEnable: true,center: [105.602725, 37.076636] // 初始化地图中心点位置})// 添加makerthis.setMaker()// 添加鼠标点选地图选择地址this.addAmapGeocoder()this.onhandelSearch()}).catch(e => {console.log(e)})},// 添加makersetMaker() {// eslint-disable-next-line no-undefthis.marker = new AMap.Marker()this.map.add(this.marker)// 添加解析地理位置插件this.map.plugin('AMap.Geocoder', () => {// 异步加载插件this.geocoder = new AMap.Geocoder({city: this.defaultCity, // 默认:“全国”radius: 1000 // 范围,默认:500})})},// 添加鼠标点选地图选择地址addAmapGeocoder() {// 添加makerthis.setMaker()// 地图添加点击事件this.map.on('click', function(e) {console.log('e.lnglat.getLng()', e.lnglat.getLng())// document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat()})this.map.on('click', e => {console.log('e====', e)const lnglat = [e.lnglat.getLng(), e.lnglat.getLat()]this.marker.setPosition(lnglat)this.geocoder.getAddress(lnglat, (status, result) => {if (status === 'complete' && result.regeocode) {const res = result.regeocodeconst { adcode, province, city, district } = res.addressComponentthis.searchValue = res.formattedAddressconst name = province + city + districtconst sdata = { adcode, lng: lnglat[0], lat: lnglat[1], name }this.searchSuccessData(sdata)console.log('result', result)} else {console.log(JSON.stringify(result))}})})},// 按钮触发检索onhandelSearch() {const that = thisthis.geocoder.getLocation(this.searchValue, function(status, result) {if (status === 'complete' && result.geocodes.length) {const { lng, lat } = result.geocodes[0].locationconst { province, city, district } = result.geocodes[0].addressComponentconst { adcode } = result.geocodes[0]const name = province + city + districtconst sdata = { adcode, lng, lat, name }that.searchSuccessData(sdata)that.marker.setPosition([lng, lat])that.map.add(that.marker)that.map.setFitView(that.marker)} else {this.$message.error('根据地址查询位置失败')}})},searchSuccessData(res) {this.formattedAddress = this.searchValuethis.adcode = res.adcodethis.name = res.namethis.position = { lng: res.lng, lat: res.lat }},// 清除input里的值,清除搜索结果,再次初始化maphandelclearInput() {document.querySelector('#searchResultPanel').innerHTML = ''},// 保存当前选择的地址,分发事件handelSave() {this.searchValue = this.formattedAddressconst { lat, lng } = this.positionif (lat && lng) {const data = {name: this.name,adcode: this.adcode,// 地址名称address: this.formattedAddress,// 纬度latlat,// 经度lnglng}this.$emit('getPosition', true, data)} else {this.$message.error('请选择地址获取经纬度')}},cancel() {this.$emit('getPosition', false)}}
}
</script><style scoped lang="scss">
#amap-container {margin: 20px;.el-input__clear {line-height: 34px;/*top: 20px;*/}#custom-amap {height: 30vh;width: 100%;margin-top: 10px;border: 1px solid #ccc;}.input-with {/*position: fixed;*//*top: 40px;*/z-index: 1;width: 580px;}.address {color: #373737;}}.amap-sug-result {z-index: 99999;
}
</style>

然后在需要的文件里引入就可以:
当我点击这个输入框时,就会弹出地图组件
在这里插入图片描述

这个是地图组件:在这里插入图片描述
引用组件的代码如下:

 <el-input v-model="basicFormValidate.businessAddressDetail" @click.native="initMapConfirm" /><amap :visible="amapVisible" :default-value="basicFormValidate.businessAddressDetail" :business-province-id="basicFormValidate.businessProvinceId" @getPosition="mapConfirm" />import Amap from '@/views/common/Amap'components: { Amap }initMapConfirm() {this.amapVisible = true},mapConfirm(flag, data) {this.amapVisible = falseif (flag) {this.basicFormValidate.businessAddressDetail = data.addressthis.basicFormValidate.businessAddressLatitude = data.latthis.basicFormValidate.businessAddressLongitude = data.lngthis.basicFormValidate.businessProvinceId = data.businessProvinceId}}

最后的结果就是这样的
在这里插入图片描述
如果说之前有地址,那会代入并反向定位,获取其经纬度

在这里插入图片描述
好了就分享到这儿,备个份,助人达己。
如果说有更好的方便,欢迎交流分享。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_289560.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20230420使用逻辑分析仪测量摄像头的PAG7920的时钟信号

20230420使用逻辑分析仪测量摄像头的PAG7920的时钟信号 2023/4/20 19:14 在CV1826平台&#xff1a; 1、vsync信号&#xff1a;刷新率120HZ PAG7920LT: Ultra-Low Power Global Shutter Image Sensor Max. Frame Rate 180 FPS 20KSa/20KHZ 2、href行同步信号&#xff1a;KHZ级别…

Vulnhub项目:JANGOW 1.0.1

靶机地址&#xff1a;Jangow: 1.0.1 ~ VulnHub 渗透过程&#xff1a; kali ip&#xff1a;192.168.56.104&#xff0c;使用arp-scan -l查看到靶机ip192.168.56.118 对靶机进行端口探测&#xff0c;发现了21、80端口 访问80端口&#xff0c;发现site目录 点击进去后&#xff0…

【Linux】使用systemd设置开机自启动命令

目录 1 使用使用systemd实现开机自动运行命令1.1 新建一个.service文件1.2 编写.service文件1.2.1 [Unit]1.2.2 [Service]1.2.3 [Install] 1.3 启动服务并设置自启动 2 编写Systemd服务文件的要点2.1 Systemd服务文件的位置2.2 Systemd服务文件的格式2.3 Systemd服务文件的基本…

Spring事务(3)-TransactionInterceptor实际事务执行

Spring事务&#xff08;2&#xff09;-EnableTransactionManagement实现源码解析 中介绍了Spring事务开启和代理的实现&#xff0c;现在了解实际事务执行TransactionInterceptor。 TransactionInterceptor TransactionInterceptor类图 MethodInterceptor&#xff1a;AOP代理后…

IDEA社区版搭建Tomcat服务器并创建web项目

IDEA社区版搭建Tomcat服务器并创建web项目 目标 创建Web项目的目录结构可以启动Tomcat服务器编写Servlet并访问成功 问题 IDEA社区版没有创建Web工程的选项IDEA社区版没有Tomcat插件 实现步骤 针对以上两个问题&#xff0c;分步解决 问题一&#xff1a;IDEA社区版没有创建…

深入认识VirtualPrivateNetwork

目录 一、认识什么是认证&#xff1f; 1.什么是数据认证&#xff0c;有什么作用&#xff0c;有哪些实现的技术手段? 2.什么是身份认证&#xff0c;有什么作用&#xff0c;有哪些实现的技术手段? 二、认识什么是VPN 1.什么VPN技术? 2.VPN技术有哪些分类? 3.IPSEC技术…

我的Qt作品(18)模仿Qt Creator IDE写了一个轻量级的视觉框架

Qt Creator的源码比较庞大。前几年我陆陆续续读过里面的源码。也写了几篇博文&#xff1a; https://blog.csdn.net/libaineu2004/article/details/104728857 https://blog.csdn.net/libaineu2004/article/details/89407333 最近一直想找机会&#xff0c;借用这个IDE的皮&…

mapreduce基础: 手写wordcount案例

文章目录 一、源代码二、运行截图 一、源代码 WordCountMapper类 package org.example.wordcount;import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper;impo…

DNS服务器配置与使用【CentOS】

从本质上说&#xff0c;DNS是一个分布数据库&#xff0c;是一个树形结构&#xff08;不是网状&#xff09;——层次结构 DNS查找过程就是 回溯的过程&#xff08;递归、迭代&#xff09; www.xxx.edu.cn&#xff08;属于四层结构&#xff09; 查询DNS&#xff1a;域名到IP地址的…

【Maven 入门】第二章、Maven核心程序解压与配置

一、Maven 官网地址 首页&#xff1a; Maven – Welcome to Apache Maven(opens new window) 下载页面&#xff1a; Maven – Download Apache Maven(opens new window) 本文以maven-3.3.8为例 具体下载地址&#xff1a;https://dlcdn.apache.org/maven/maven-3/3.8.8/bina…

Linux学习记录—— 이십일 进程间通信(3)信号量和消息队列

文章目录 1、消息队列2、信号量1、了解概念2、信号量理解 3、接口4、理解IPC 1、消息队列 两个进程ab之间系统维护一个队列结构&#xff0c;a进程往队列里放信息&#xff0c;信息编号为1&#xff0c;b进程往队列里放信息&#xff0c;信息编号为2&#xff1b;之后开始读取数据的…

OrCAD原理图检查

OrCAD原理图检查 FPGA或处理器芯片原理图封装检查OrCad元件Part Reference与Reference位号不同检查所有器件是否与CIS库元件匹配用CIS库中的元器件替换已存在器件方法1方法2 DRC检查修改页码Annotate重排位号利用Intersheet References功能进行off-page索引检查封装、厂家、型号…

追梦之旅【数据结构篇】——看看小白试如何利用C语言“痛”撕堆排序

追梦之旅【数据结构篇】——看看小白试如何利用C语言“痛”撕堆排序 ~&#x1f60e; 前言&#x1f64c;堆的应用 —— 堆排序算法&#xff1a;堆排序算法源代码分享运行结果测试截图&#xff1a; 总结撒花&#x1f49e; &#x1f60e;博客昵称&#xff1a;博客小梦 &#x1f60…

安装配置 JupyterLab ubuntu20.04

目录 ​编辑 &#xff08;1&#xff09;安装 &#xff08;2&#xff09;配置 &#xff08;1&#xff09;生成配置文件 &#xff08;2&#xff09;生成jupyterlab的登录密码 &#xff08;3&#xff09;修改 jupyter 的配置文件 &#xff08;4&#xff09;安装 jupyterlab…

leetcode每日一题——美团笔试题【1】

今天分享两道算法题&#xff0c;自己刚开始练习&#xff0c;可能在解法上不是最佳的&#xff0c;但是只提供一些自己的思路&#xff0c;欢迎大家多多指教~ 第一题 实现一个算法&#xff0c;确定一个字符串 s 的所有字符是否全都不同。 示例 1&#xff1a;输入: s "lee…

算法的时间复杂度和空间复杂度(2)

计算斐波那契递归Fib的时间复杂度&#xff1f; long long Fib(size_t N) { if(N < 3) return 1; return Fib(N-1) Fib(N-2); } 因为递归先递推后回归&#xff0c;看起来规律像等比数列&#xff0c;也可以用错位相减法&#xff0c;因为斐波那契数列到第二项就不会再计算了&a…

【Spring Boot】SpringBoot设计了哪些可拓展的机制?

文章目录 前言SpringBoot核心源码拓展Initializer拓展监听器ApplicationListenerBeanFactory的后置处理器 & Bean的后置处理器AOP其他的拓展点 前言 当我们引入注册中心的依赖&#xff0c;比如nacos的时候&#xff0c;当我们启动springboot&#xff0c;这个服务就会根据配置…

2023/4/20总结

项目 网上关于listview的资料太少了&#xff0c;在网上的那些资料里面&#xff0c;了解到以下这些。 如果希望listview后期能更改或者更新&#xff0c;那么需要使用到 ObservableList 它可以观察到&#xff0c;listview的改动。 需要特别注意一点的是&#xff1a;写俩者的…

第 三 章 UML 类图

文章目录 前言一、依赖关系&#xff08;虚线箭头&#xff09;二、泛化关系&#xff1a;继承&#xff08;实线空心箭头&#xff09;三、实现关系&#xff08;虚线空心箭头&#xff09;四、关联关系&#xff08;一对一为实线箭头&#xff0c;一对多为实线&#xff09;五、聚合关系…

java贸易企业工作信息管理与利润返现系统sxA5进销存程序

目 录 摘 要 I Abstract II 第1章 绪论 1 1.1 课题背景 1 1.2 研究现状 1 本章小结 1 第2章 可行性分析 2 2.1 经济可行性 2 2.2 技术可行性 2 2.3 操作可行性 2 2.4 业务流程分析 3 本章小结 3 第3章 需求分析 4 3.1 需求分析 4 …