Vue.js 加入高德地圖的實(shí)現(xiàn)代碼
一、功能需求
1.根據(jù)輸入內(nèi)容進(jìn)行模糊查詢,選擇地址后在地圖上插上標(biāo)記,并更新經(jīng)緯度坐標(biāo)顯示
2.在地圖點(diǎn)擊后,根據(jù)回傳的左邊更新地址信息和坐標(biāo)顯示
二、準(zhǔn)備
1.申請(qǐng)高德地圖賬號(hào),創(chuàng)建應(yīng)用
2.在應(yīng)用管理中 獲得key 和安全密鑰
三、在webstorm中安裝
npm i @amap/amap-jsapi-loader -S
四、防止在使用中AMap無(wú)法識(shí)別問(wèn)
在eslintrc.js中加入配置:
globals:{ "AMap": "true" }
五、正式開(kāi)發(fā)
1.創(chuàng)建頁(yè)面
<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="請(qǐng)輸入搜索地址" 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="經(jīng)度:" > <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.頁(yè)面樣式
<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.存儲(chǔ)的數(shù)據(jù)項(xiàng)
data () { return { map: null, marker: null, startSeacrh: [], stratInfo: {}, dprops: { zoom: 15 }, formatAdress: '', longitude: '', // 經(jīng)度 latitude: '', // 緯度 } }
4.創(chuàng)建地圖方法
mounted () { this.initMap() }, methods: { initMap () { const that = this init('allmap', that.dprops).then(AMap => { that.map = AMap that.map.on('click', that.clickHandler) // 地圖點(diǎn)擊事件 可獲取經(jīng)緯度等信息 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, '起點(diǎn)經(jīng)緯度 [lng,lat]') if (event.lnglat === '') { this.$message({ type: 'warning', message: '該地點(diǎn)無(wú)經(jīng)緯度數(shù)據(jù),請(qǐng)輸入具體一點(diǎn)的地點(diǎn)!', 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, '起點(diǎn)經(jīng)緯度 [lng,lat]') if (event.poi.location === '') { this.$message({ type: 'warning', message: '該地點(diǎn)無(wú)經(jīng)緯度數(shù)據(jù),請(qǐng)輸入具體一點(diǎn)的地點(diǎn)!', 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 地圖實(shí)例 * @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: '全國(guó)' } let startAutoComplete = new AMap.Autocomplete(autoOptions1) AMap.PlaceSearch({ map: map }) AMap.event.addListener(startAutoComplete, 'select', commpletHandle) }) } /** * * @param {String} LngLat 經(jīng)緯度 * @param {Function} callback 回調(diào)函數(shù) * @param {Object} otherProps 其他參數(shù) */ 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
在文件中導(dǎo)入 map.js方法
import { init, initScaleTools, searchAutocomplete, getAddressByLngLat } from '../../utils/map'
六、步驟總結(jié)
1.在mounted()中調(diào)用 initMap ()初始化地圖
2.初始化成功后、添加事件監(jiān)聽(tīng):地圖點(diǎn)擊、模糊查詢。添加放大縮小工具欄
init('allmap', that.dprops).then(AMap => { that.map = AMap that.map.on('click', that.clickHandler) // 地圖點(diǎn)擊事件 可獲取經(jīng)緯度等信息 initScaleTools(that.map, true, false) searchAutocomplete(that.map, 'tipinput', function (event) { that.handleStartSelect(event) }) })
七:效果
到此這篇關(guān)于Vue.js 加入高德地圖的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Vue.js 加入高德地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何解決sass-loader的版本過(guò)高導(dǎo)致的編譯錯(cuò)誤
這篇文章主要介紹了vue如何解決sass-loader的版本過(guò)高導(dǎo)致的編譯錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06VUE中computed 、created 、mounted的先后順序說(shuō)明
這篇文章主要介紹了VUE中computed 、created 、mounted的先后順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Vue中import from@符號(hào)指的是什么意思
這篇文章主要介紹了Vue中import from@符號(hào)指的是意思,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03安裝vue3開(kāi)發(fā)者工具但控制臺(tái)沒(méi)有顯示出vue選項(xiàng)的解決
這篇文章主要介紹了安裝vue3開(kāi)發(fā)者工具但控制臺(tái)沒(méi)有顯示出vue選項(xiàng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue項(xiàng)目代碼格式規(guī)范設(shè)置參考指南
這篇文章主要給大家介紹了關(guān)于vue3簡(jiǎn)單封裝input組件和統(tǒng)一表單數(shù)據(jù)的相關(guān)資料,不管你學(xué)習(xí)哪一門編程語(yǔ)言,相信大家都會(huì)略化這一部分,需要的朋友可以參考下2022-05-05vue3中v-for報(bào)錯(cuò)'item'is?of?type'unknown'的
在寫vue3+ts的項(xiàng)目,得到一個(gè)數(shù)組,需要循環(huán)展示,使用v-for循環(huán),寫完之后發(fā)現(xiàn)有個(gè)報(bào)錯(cuò),接下來(lái)通過(guò)本文給大家介紹vue3中v-for報(bào)錯(cuò)?‘item‘?is?of?type?‘unknown‘的解決方法,感興趣的朋友一起看看吧2023-11-11