Vue.js 加入高德地图的实现代码
作者:清雨未尽时 发布时间:2024-05-28 15:51:31
标签:Vue.js,高德地图
一、功能需求
1.根据输入内容进行模糊查询,选择地址后在地图上插上标记,并更新经纬度坐标显示
2.在地图点击后,根据回传的左边更新地址信息和坐标显示
二、准备
1.申请高德地图账号,创建应用
2.在应用管理中 获得key 和安全密钥
三、在webstorm中安装
npm i @amap/amap-jsapi-loader -S
四、防止在使用中AMap无法识别问
在eslintrc.js中加入配置:
globals:{
"AMap": "true"
}
五、正式开发
1.创建页面
<template>
<div>
<label>消息管理</label>
<div style="margin-top: 20px">
<div style="height:520px;">
<div id="all" style="height:100%">
<div class="posInput">
<el-input style="width:100%"
id="tipinput"
class="form-control input-style"
type="text"
placeholder="请输入搜索地址"
prefix-icon="el-icon-search"
v-model="formatAdress"
>
</el-input>
</div>
<div id="allmap"></div>
<div class="posSubmit">
<el-form ref="form" label-width="85px" >
<div class="btn_box" >
<el-form-item label="经度:" >
<el-input style="width:400px" disabled class="form-control input-style" type="text" v-model="longitude"> </el-input>
</el-form-item>
<el-form-item label="纬度:" >
<el-input style="width:400px" disabled class="form-control input-style" type="text" v-model="latitude"> </el-input>
</el-form-item>
</div>
</el-form>
</div>
</div>
</div>
</div>
</div>
</template>
2.页面样式
<style scoped>
#all{
position: relative;
}
#allmap{
width: 100%; height: calc(100% - 50px);
font-family: "微软雅黑";
}
.posInput{
position: absolute;
z-index: 1;
width: 80%;
margin-top: 20px; margin-left: 10%;
}
.posSubmit{
position: absolute; z-index: 1; bottom: 0;
margin-left: 5%;
width: 90%;
display: flex; justify-content: flex-start; align-items: center;
}
.btn_box{
width: 100%;
height: 100%;
display: flex; ; align-items: center;
}
::v-deep .el-form-item{
margin-bottom: 0 !important;
}
</style>
3.存储的数据项
data () {
return {
map: null,
marker: null,
startSeacrh: [],
stratInfo: {},
dprops: {
zoom: 15
},
formatAdress: '',
longitude: '', // 经度
latitude: '', // 纬度
}
}
4.创建地图方法
mounted () {
this.initMap()
},
methods: {
initMap () {
const that = this
init('allmap', that.dprops).then(AMap => {
that.map = AMap
that.map.on('click', that.clickHandler) // 地图点击事件 可获取经纬度等信息
initScaleTools(that.map, true, false)
searchAutocomplete(that.map, 'tipinput', function (event) {
that.handleStartSelect(event)
})
}).catch(err => {
this.$message.error(err)
})
},
clickHandler (event) {
console.log(event, '起点经纬度 [lng,lat]')
if (event.lnglat === '') {
this.$message({
type: 'warning',
message: '该地点无经纬度数据,请输入具体一点的地点!',
duration: 5 * 1000
})
return
}
if (this.marker) {
this.map.remove(this.marker)
this.marker = null
}
this.startSeacrh = []
this.startSeacrh = [event.lnglat.lng, event.lnglat.lat]
this.marker = new AMap.Marker({
position: this.startSeacrh
})
this.map.add(this.marker)
this.map.setCenter(this.startSeacrh)
this.longitude = event.lnglat.lng
this.latitude = event.lnglat.lat
let that = this
getAddressByLngLat(this.startSeacrh, function (status, result) {
if (status === 'complete') {
that.formatAdress = result.regeocode.formattedAddress
let adrComponent = result.regeocode.addressComponent
that.stratInfo = {
district: adrComponent.province,
address: adrComponent.district,
name: adrComponent.township + adrComponent.street + adrComponent.streetNumber,
fullAdr: result.regeocode.formattedAddress
}
}
})
},
handleStartSelect (event) {
console.log(event, '起点经纬度 [lng,lat]')
if (event.poi.location === '') {
this.$message({
type: 'warning',
message: '该地点无经纬度数据,请输入具体一点的地点!',
duration: 5 * 1000
})
return
}
if (this.marker) {
this.map.remove(this.marker)
this.marker = null
}
this.startSeacrh = []
this.startSeacrh = [event.poi.location.lng, event.poi.location.lat]
this.formatAdress = event.poi.district + event.poi.address + event.poi.name
this.longitude = event.poi.location.lng
this.latitude = event.poi.location.lat
this.marker = new AMap.Marker({
position: this.startSeacrh
})
this.map.add(this.marker)
this.map.setCenter(this.startSeacrh)
this.stratInfo = {
district: event.poi.district,
address: event.poi.address,
name: event.poi.name,
fullAdr: this.formatAdress
}
}
}
5.封装好的js文件方法
initMap.js
import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
securityJsCode: '安全密钥'
}
const initMap = async (config) => {
return new Promise((resolve, reject) => {
AMapLoader.load({
'key': config.key,
'version': '1.4.15',
'plugins': [
'AMap.PolygonEditor' // 插件
],
'AMapUI': {
'version': '1.1',
'plugins': []
},
'Loca': {
'version': '1.3.2'
}
}).then((AMap) => {
resolve(AMap)
}).catch(err => {
reject(err)
})
})
}
export default initMap
map.js
import initMap from './initMap.js'
export const init = (container, props) => {
const config = {
key: 'key'
}
return new Promise((resolve, reject) => {
initMap(config).then(AMap => {
resolve(new AMap.Map(container, { ...props }))
}).catch(err => {
reject(err)
})
})
}
/**
* @param {*} map 地图实例
* @param {Boolean} noScale 不需要比例尺 true表示不需要
* @param {Boolean} noToolBar 不需要工具栏 true表示不需要
*/
export const initScaleTools = (map, noScale, noToolBar) => {
if (!noScale) {
map.plugin(['AMap.Scale'], function () {
var scale = new AMap.Scale()
map.addControl(scale)
})
}
if (!noToolBar) {
map.plugin(['AMap.ToolBar'], function () {
var tool = new AMap.ToolBar()
map.addControl(tool)
})
}
}
//模糊查询
export const searchAutocomplete = (map, keyword, commpletHandle) => {
map.clearMap()
AMap.plugin(['AMap.PlaceSearch', 'AMap.Autocomplete'], function () {
let autoOptions1 = { input: keyword, city: '全国' }
let startAutoComplete = new AMap.Autocomplete(autoOptions1)
AMap.PlaceSearch({
map: map
})
AMap.event.addListener(startAutoComplete, 'select', commpletHandle)
})
}
/**
*
* @param {String} LngLat 经纬度
* @param {Function} callback 回调函数
* @param {Object} otherProps 其他参数
*/
export const getAddressByLngLat = (LngLat, callback, otherProps) => {
AMap.plugin('AMap.Geocoder', function () {
let geocoder = new AMap.Geocoder({
...otherProps
})
geocoder.getAddress(LngLat, function (status, result) {
callback(status, result)
})
})
}
const mapJS = {
init,
initScaleTools,
searchAutocomplete,
getAddressByLngLat
}
export default mapJS
在文件中导入 map.js方法
import {
init,
initScaleTools,
searchAutocomplete,
getAddressByLngLat
} from '../../utils/map'
六、步骤总结
1.在mounted()中调用 initMap ()初始化地图
2.初始化成功后、添加事件监听:地图点击、模糊查询。添加放大缩小工具栏
init('allmap', that.dprops).then(AMap => {
that.map = AMap
that.map.on('click', that.clickHandler) // 地图点击事件 可获取经纬度等信息
initScaleTools(that.map, true, false)
searchAutocomplete(that.map, 'tipinput', function (event) {
that.handleStartSelect(event)
})
})
七:效果
来源:https://blog.csdn.net/kangguang/article/details/128096331


猜你喜欢
- 事实上,当我们向文件导入某个模块时,导入的是
- 为什么要问如何存储IP?首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗?其实做任何程序设计都要在功能
- 在我们看一些使用反射的代码的时候,会发现,reflect.ValueOf 或 reflect.TypeOf 的参数有些地方使用的是指针参数,
- 前言之前公司设计的网站比较混乱,很多地方不统一,其中一个就是弹出层,导致这个原因是因为,公司的UI换了好几个人,而他们每个人做出来的都不太一
- 本文实例讲述了python实现的简单FTP上传下载文件的方法。分享给大家供大家参考。具体如下:python本身自带一个FTP模块,可以实现上
- 远程登陆SQLServer (2014)数据库,供大家参考,具体内容如下两台电脑,同一个局域网内,IP同一网段配置:Computer1: W
- 目录HDFS NameNode 高可用Hadoop Namenode 高可用架构Namenode 高可用的实现隔离(Fencing)QJM共
- 作者:xiaolanLin声明 :本文版权归作者和博客园共有,来源网址:https://www.cnblogs.com/xiaolan-Li
- //香水坏坏 AT 06-07-25 //郁闷的事情总是接连不断,无形的压力来自内心的恐惧 大家在用.net进行数据操作
- 1 捕捉一个异常捕捉一个异常 以用0作为除数会得到ZeroDivisionError异常为例,print(1/0)为例程序的持续执行,不因该
- 最近做个软件,用PyQT写的,在实现菜单栏点击弹出新窗口的时候严重被卡壳,发现用WxPython的思想和方式来做完全无法实现。PyQT的中文
- 本文实例讲述了C#实现按数据库邮件列表发送邮件的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 子类在多继承中使用MRO机制在Python中,当定义一个类时,可以指定它的父类。一个子类继承了其所有父类的属性和方法,并且可以添加自己特有的
- 字典的键 字典中的值没有任何限制, 可以是任意Python对象,即从
- python脚本替换指定行实现步骤 本文主要介
- 目录前言Tips - django版本区别路由匹配无名分组&有名分组无名分组有名分组小提示反向解析路由不涉及分组的反向解析有名分组&
- 注:本文的所有数据请移步—— 参考数据一、水平堆叠图堆叠图其实就是柱状图的一种特殊形式fr
- 1.在 utils 文件中新建 mcaptcha.js 文件,写入以下代码:module.exports = class Mcaptcha
- 本例使用 QQ邮箱测试,需要打开 QQ邮箱的 smtp协议,获取授权码代码内容如下:#!/usr/bin/env python# _*_ c
- 【译者的话】 网页上的小广告(banner)已经成为一种宣传推广的重要形式,但这些小广告除了版面细小外,图象的表现还受到象素较低等其它因素影