vue中調(diào)用百度地圖獲取經(jīng)緯度的實(shí)現(xiàn)
項(xiàng)目中,需要實(shí)現(xiàn)獲取當(dāng)前位置的經(jīng)緯度,或者搜索某個(gè)位置并獲取經(jīng)緯度信息,我使用的的是vue,地圖使用的是百度地圖。
默認(rèn)自動(dòng)獲取當(dāng)前位置經(jīng)緯度
拖動(dòng)小紅標(biāo) 獲取經(jīng)緯度
關(guān)鍵詞 查詢獲取經(jīng)緯度
前期準(zhǔn)備
首先,我們需要取百度官方申請(qǐng)一個(gè)地圖api秘鑰,https://lbsyun.baidu.com/apiconsole/key 進(jìn)入后在應(yīng)用管理,我的應(yīng)用去申請(qǐng)即可。
申請(qǐng)好以后,我們打開vue項(xiàng)目中public文件下的index.html文件,拼接百度AK值并引入
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=WFKACU6v7aiUdnkgtMCqdWBZC68KpUXv"></script>
如上所示,紅色區(qū)域?yàn)锳K值,自行拼接自己的,可以設(shè)置權(quán)限為公開或者針對(duì)網(wǎng)址白名單。
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=WFKACU6v7aiUdnkgtMCqdWBZC68KpUXv"></script>
我使用了elementui的彈窗,輸入框,提示,如果你沒使用elementui,記得更換哦!
HTML代碼
<template> <div> <el-dialog @close="clearDialog" :close-on-click-modal="false" :title="text" style="text-align: left" :visible.sync="popup" width="30%" > <div class="form-layer"> <el-form label-width="100px" size="mini"> <el-form-item label="獲取定位"> <el-button type="primary" @click="fixedPos">重新定位</el-button> </el-form-item> <el-form-item label="當(dāng)前緯度"> <p>{{latitude}}</p> </el-form-item> <el-form-item label="當(dāng)前經(jīng)度"> <p>{{longitude}}</p> </el-form-item> <el-form-item> <div class="f-a-c"> <el-input v-model="keyWords" placeholder="請(qǐng)輸入地區(qū)" style="width: 230px;margin-right: 6px;"></el-input> <el-button type="primary" @click="setPlace" :disabled="!keyWords">查詢</el-button> </div> </el-form-item> </el-form> <div id="map"></div> </div> <div slot="footer" class="dialog-footer"> <el-button size="small" type="primary" v-if="type != '2'" @click="btnSubmit()" >確 認(rèn)</el-button > <el-button size="small" @click="popup = false">取 消</el-button> </div> </el-dialog> </div> </template>
JS代碼
<script> export default { name: "mapView", data() { return { map: null, local: null, mk: null, latitude: '', longitude: '', keyWords: '' }; }, methods: { // 打開彈窗,name為彈窗名稱 async openDialog(name) { this.text = name; this.popup = true; this.initMap(); }, // 確認(rèn) btnSubmit() { let key = { latitude: this.latitude, longitude: this.longitude } // 打印經(jīng)緯度 console.log(key); this.popup = false; }, initMap() { this.$nextTick(() => { this.map = new BMap.Map("map"); let point = new BMap.Point(116.404, 39.915); this.map.centerAndZoom(point, 12); this.map.enableScrollWheelZoom(true); // 開啟鼠標(biāo)滾輪縮放 this.map.addControl(new BMap.NavigationControl()); this.fixedPos(); }); }, // 點(diǎn)擊定位-定位到當(dāng)前位置 fixedPos() { const _this = this; const geolocation = new BMap.Geolocation(); this.confirmLoading = true; geolocation.getCurrentPosition(function (r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { _this.handleMarker(_this, r.point); let myGeo = new BMap.Geocoder(); myGeo.getLocation( new BMap.Point(r.point.lng, r.point.lat), function (result) { _this.confirmLoading = false; if (result) { _this.latitude = result.point.lat; _this.longitude = result.point.lng; } } ); } else { _this.$message.error("failed" + this.getStatus()); } }); }, // 搜索地址 setPlace() { this.local = new BMap.LocalSearch(this.map, { onSearchComplete: this.searchPlace, }); this.local.search(this.keyWords); }, searchPlace() { if (this.local.getResults() != undefined) { this.map.clearOverlays(); //清除地圖上所有覆蓋物 if (this.local.getResults().getPoi(0)) { let point = this.local.getResults().getPoi(0).point; //獲取第一個(gè)智能搜索的結(jié)果 this.map.centerAndZoom(point, 18); this.handleMarker(this, point); console.log("經(jīng)度:" + point.lng + "--" + "緯度" + point.lat); this.latitude = point.lat; this.longitude = point.lng; } else { this.$message.error("未匹配到地點(diǎn)!"); } } else { this.$message.error("未找到搜索結(jié)果!"); } }, // 設(shè)置標(biāo)注 handleMarker(obj, point) { let that = this; obj.mk = new BMap.Marker(point); obj.map.addOverlay(obj.mk); obj.mk.enableDragging(); // 可拖拽 obj.mk.addEventListener("dragend", function (e) { // 監(jiān)聽標(biāo)注的拖拽,獲取拖拽后的經(jīng)緯度 that.latitude = e.point.lat; that.longitude = e.point.lng; }); obj.map.panTo(point); }, } }; </script>
CSS代碼
<style scoped> .form-layer { width: 100%; } #map { margin-top: 30px; width: 100%; height: 300px; border: 1px solid gray; box-sizing: border-box; overflow: hidden; } /deep/ .el-dialog { min-width: 550px; } /deep/ .el-dialog__body { padding: 10px; } </style>
完整代碼
<template> <div> <el-dialog @close="clearDialog" :close-on-click-modal="false" :title="text" style="text-align: left" :visible.sync="popup" width="30%" > <div class="form-layer"> <el-form label-width="100px" size="mini"> <el-form-item label="獲取定位"> <el-button type="primary" @click="fixedPos">重新定位</el-button> </el-form-item> <el-form-item label="當(dāng)前緯度"> <p>{{latitude}}</p> </el-form-item> <el-form-item label="當(dāng)前經(jīng)度"> <p>{{longitude}}</p> </el-form-item> <el-form-item> <div class="f-a-c"> <el-input v-model="keyWords" placeholder="請(qǐng)輸入地區(qū)" style="width: 230px;margin-right: 6px;"></el-input> <el-button type="primary" @click="setPlace" :disabled="!keyWords">查詢</el-button> </div> </el-form-item> </el-form> <div id="map"></div> </div> <div slot="footer" class="dialog-footer"> <el-button size="small" type="primary" v-if="type != '2'" @click="btnSubmit()" >確 認(rèn)</el-button > <el-button size="small" @click="popup = false">取 消</el-button> </div> </el-dialog> </div> </template> <script> export default { name: "mapView", data() { return { map: null, local: null, mk: null, latitude: '', longitude: '', keyWords: '' }; }, methods: { // 打開彈窗,name為彈窗名稱 async openDialog(name) { this.text = name; this.popup = true; this.initMap(); }, // 確認(rèn) btnSubmit() { let key = { latitude: this.latitude, longitude: this.longitude } // 打印經(jīng)緯度 console.log(key); this.popup = false; }, initMap() { this.$nextTick(() => { this.map = new BMap.Map("map"); let point = new BMap.Point(116.404, 39.915); this.map.centerAndZoom(point, 12); this.map.enableScrollWheelZoom(true); // 開啟鼠標(biāo)滾輪縮放 this.map.addControl(new BMap.NavigationControl()); this.fixedPos(); }); }, // 點(diǎn)擊定位-定位到當(dāng)前位置 fixedPos() { const _this = this; const geolocation = new BMap.Geolocation(); this.confirmLoading = true; geolocation.getCurrentPosition(function (r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { _this.handleMarker(_this, r.point); let myGeo = new BMap.Geocoder(); myGeo.getLocation( new BMap.Point(r.point.lng, r.point.lat), function (result) { _this.confirmLoading = false; if (result) { _this.latitude = result.point.lat; _this.longitude = result.point.lng; } } ); } else { _this.$message.error("failed" + this.getStatus()); } }); }, // 搜索地址 setPlace() { this.local = new BMap.LocalSearch(this.map, { onSearchComplete: this.searchPlace, }); this.local.search(this.keyWords); }, searchPlace() { if (this.local.getResults() != undefined) { this.map.clearOverlays(); //清除地圖上所有覆蓋物 if (this.local.getResults().getPoi(0)) { let point = this.local.getResults().getPoi(0).point; //獲取第一個(gè)智能搜索的結(jié)果 this.map.centerAndZoom(point, 18); this.handleMarker(this, point); console.log("經(jīng)度:" + point.lng + "--" + "緯度" + point.lat); this.latitude = point.lat; this.longitude = point.lng; } else { this.$message.error("未匹配到地點(diǎn)!"); } } else { this.$message.error("未找到搜索結(jié)果!"); } }, // 設(shè)置標(biāo)注 handleMarker(obj, point) { let that = this; obj.mk = new BMap.Marker(point); obj.map.addOverlay(obj.mk); obj.mk.enableDragging(); // 可拖拽 obj.mk.addEventListener("dragend", function (e) { // 監(jiān)聽標(biāo)注的拖拽,獲取拖拽后的經(jīng)緯度 that.latitude = e.point.lat; that.longitude = e.point.lng; }); obj.map.panTo(point); }, } }; </script> <style scoped> .form-layer { width: 100%; } #map { margin-top: 30px; width: 100%; height: 300px; border: 1px solid gray; box-sizing: border-box; overflow: hidden; } /deep/ .el-dialog { min-width: 550px; } /deep/ .el-dialog__body { padding: 10px; } </style>
到此這篇關(guān)于vue中調(diào)用百度地圖獲取經(jīng)緯度的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue調(diào)用百度地圖獲取經(jīng)緯度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element創(chuàng)建動(dòng)態(tài)的form表單及動(dòng)態(tài)生成表格的行和列
這篇文章主要介紹了vue+element創(chuàng)建動(dòng)態(tài)的form表單及動(dòng)態(tài)生成表格的行和列 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05vue使用Element-UI部分組件適配移動(dòng)端問(wèn)題
這篇文章主要介紹了vue使用Element-UI部分組件適配移動(dòng)端問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue 動(dòng)態(tài)添加/刪除dom元素節(jié)點(diǎn)的操作代碼
這篇文章主要介紹了vue 動(dòng)態(tài)添加/刪除dom元素,需要在點(diǎn)擊添加時(shí),增加一行key/value的輸入框;點(diǎn)擊垃圾桶圖標(biāo)時(shí),刪除對(duì)應(yīng)行,本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12vue-router權(quán)限控制(簡(jiǎn)單方式)
這篇文章主要介紹了vue-router權(quán)限控制(簡(jiǎn)單方式),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10vue項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
離放假還有1天,今天小編抽空給大家分享前端時(shí)間小編做的vue項(xiàng)目,非常完整,需要的朋友參考下2018-02-02vue配置請(qǐng)求本地json數(shù)據(jù)的方法
這篇文章主要介紹了vue配置請(qǐng)求本地json數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04如何在Vue.js中實(shí)現(xiàn)標(biāo)簽頁(yè)組件詳解
這篇文章主要給大家介紹了關(guān)于如何在Vue.js中實(shí)現(xiàn)標(biāo)簽頁(yè)組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問(wèn)題
這篇文章主要介紹了vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10