Vue集成百度地圖實(shí)現(xiàn)位置選擇功能
Vue集成百度地圖實(shí)現(xiàn)位置選擇
- 需求:添加門店時(shí),需要選擇門店的省、市、區(qū)、詳細(xì)地址、以及門店的經(jīng)緯度信息。
- 解決方案:集成百度地圖API,通過在地圖上搜索或者點(diǎn)擊獲取門店的具體位置信息。
百度地圖選擇地址效果
具體效果如下圖所示
集成百度地圖的具體實(shí)現(xiàn)
技術(shù)方案: Vue + ElementUI + 百度地圖 JavaScript API v3.0
工程目錄結(jié)構(gòu)圖:
``` ProjectName // 項(xiàng)目名稱 ├── public // 公共文件 │ └── index.html // html模板 ├── src // 源代碼 │ └── components // 全局共用組件 │ └── BMapAddressSelect // 百度地圖位置選擇組件 │ └── views // 頁面文件
- 申請成為百度地圖開發(fā)者并獲取秘鑰
參考文檔:https://lbsyun.baidu.com/index.php?title=jspopular3.0/guide/getkey
第一步:引入百度地圖 JavaScript API v3.0 文件
修改文件:public -> index.html
<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=你的秘鑰"></script>
第二步:編寫百度地圖選擇位置組件
新增文件:src -> components -> BMapAddressSelect -> index.vue
<template> <el-dialog title="位置選擇" :visible.sync="openMap" width="900px" append-to-body> <el-form label-width="80px"> <el-row> <el-col :span="10"> <el-form-item label="搜索地址"> <el-input size="mini" type="text" id="searchAddres" v-model="searchAddresKeywords" placeholder="請輸入地點(diǎn)"> </el-input> </el-form-item> </el-col> <el-col :span="14"> <el-form-item label="當(dāng)前地址"> <el-input placeholder="請輸入內(nèi)容" v-model="addressInfo.address"> <template slot="prepend">{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.district}}</template> </el-input> </el-form-item> </el-col> </el-row> </el-form> <div id="baidu-map-container" style="width: 100%; height: 400px;"></div> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="confirmSelect">確 定</el-button> <el-button @click="cancel">取 消</el-button> </div> </el-dialog> </template> <script> export default { data() { return { searchAddresKeywords: "", addressInfo: { // 地址信息 longitude: "", // 經(jīng)度 latitude: "", // 緯度 province: "", // 省 city: "", // 市 district: "", // 區(qū) address: "", // 詳細(xì)地址 }, openMap: false, }; }, methods: { // 初始化百度地圖 initBaiduMap() { let that = this; this.$nextTick(function () { /** 初始化地圖Start */ var map = new BMap.Map("baidu-map-container"); // 創(chuàng)建地圖實(shí)例 var point = new BMap.Point(116.404, 39.915); // 設(shè)置中心點(diǎn)坐標(biāo) map.centerAndZoom(point, 13); // 地圖初始化,同時(shí)設(shè)置地圖展示級別 map.enableScrollWheelZoom(true); //開啟鼠標(biāo)滾輪縮放 /** 初始化地圖End */ /** 點(diǎn)擊地圖創(chuàng)建坐標(biāo)事件Start */ // 添加地圖點(diǎn)擊事件 map.addEventListener("click", function (e) { var clickpt = e.point; // 點(diǎn)擊的坐標(biāo) map.clearOverlays(); // 移除地圖上的標(biāo)注 var marker = new BMap.Marker(clickpt); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 // 逆地址解析 that.geocAddress(clickpt); }); /** 點(diǎn)擊地圖創(chuàng)建坐標(biāo)事件End */ /** 搜索地址Start */ // 建立一個自動完成的對象 var ac = new BMap.Autocomplete({ input: "searchAddres", location: map, }); // 鼠標(biāo)點(diǎn)擊下拉列表后的事件 ac.addEventListener("onconfirm", function (e) { map.clearOverlays(); // 移除地圖上的標(biāo)注 var local = new BMap.LocalSearch(map, { //智能搜索 onSearchComplete: function (results) { let poi = results.getPoi(0); //獲取第一個智能搜索的結(jié)果 var searchpt = poi.point; // 獲取坐標(biāo) map.centerAndZoom(searchpt, 16); map.addOverlay(new BMap.Marker(searchpt)); //添加標(biāo)注 that.geocAddress(searchpt); // 逆地址解析 }, }); // 搜索詞 var searchValue = e.item.value; local.search( searchValue.province + searchValue.city + searchValue.district + searchValue.street + searchValue.business ); }); /** 搜索地址End */ }); }, /** 逆向解析地址 point */ geocAddress(point) { let that = this; var geoc = new BMap.Geocoder(); geoc.getLocation(point, function (geocInfo) { // 設(shè)置基本信息 var addressInfo = geocInfo.addressComponents; that.addressInfo.longitude = point.lng; that.addressInfo.latitude = point.lat; that.addressInfo.province = addressInfo.province; that.addressInfo.city = addressInfo.city; that.addressInfo.district = addressInfo.district; let address = addressInfo.street + addressInfo.streetNumber; if (geocInfo.surroundingPois.length > 0) { address = address + geocInfo.surroundingPois[0].title; } that.addressInfo.address = address; }); }, /** 打開地圖選擇 */ show() { this.openMap = true; this.initBaiduMap(); }, /** * 確認(rèn)選擇 */ confirmSelect() { this.$emit("confirmMapAddress", this.addressInfo); this.openMap = false; }, /** * 取消選擇 */ cancel() { this.openMap = false; } }, }; </script> <style lang="scss"> // 防止地圖自動完成的對象被遮擋 .tangram-suggestion { z-index: 9999; } </style>
第三步:使用百度地圖選擇位置組件
<template> <div class="app-container"> <el-row> <el-col :span="12"> <el-form ref="addressInfo" :model="addressInfo" :rules="rules" label-width="100px"> <el-form-item label="門店地址" prop="address"> <el-input placeholder="請選擇門店地址" v-model="addressInfo.address" disabled> <template slot="prepend">{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.district}}</template> <el-button slot="append" icon="el-icon-map" @click="showMap" type="primary">選擇地址</el-button> </el-input> </el-form-item> <el-row> <el-col :span="12"> <el-form-item label="門店經(jīng)度" prop="longitude"> <el-input v-model="addressInfo.longitude" placeholder="請輸入經(jīng)度" disabled /> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="門店緯度" prop="latitude"> <el-input v-model="addressInfo.latitude" placeholder="請輸入緯度" disabled /> </el-form-item> </el-col> </el-row> </el-form> </el-col> </el-row> <!-- 百度地圖位置選擇 --> <BMapAddressSelect ref="bmapAddressSelect" @confirmMapAddress="confirmMapAddress"></BMapAddressSelect> </div> </template> <script> import BMapAddressSelect from "@/components/BMapAddressSelect/index"; export default { data() { return { // 位置信息 addressInfo: {}, // 表單校驗(yàn) rules: {}, }; }, components: { BMapAddressSelect, }, methods: { /** 顯示地圖 */ showMap() { this.$refs.bmapAddressSelect.show(); }, /** 確認(rèn)地圖地址 */ confirmMapAddress(addressInfo) { this.addressInfo = addressInfo; }, }, }; </script>
百度地圖Web開發(fā)
- JavaScript API v3.0 (2D地圖 標(biāo)準(zhǔn)版)
- API文檔地址:https://lbsyun.baidu.com/index.php?title=jspopular3.0
- 示例地址:https://lbsyun.baidu.com/index.php?title=open/jsdemo3.0
- JavaScript API v2.0 (沒有維護(hù)了)
- JavaScript API v1.0(沒有維護(hù)了)
- JavaScript API Lite v1.0 (2D地圖 移動端H5版)
- API文檔地址:https://lbsyun.baidu.com/index.php?title=jspopularLiteV1
- 示例地址:https://lbsyun.baidu.com/index.php?title=open/jsdemoLite
- JavaScript API GL v1.0 (3D地圖 )
- API文檔地址:https://lbsyun.baidu.com/index.php?title=jspopularGL
- 示例地址:https://lbsyun.baidu.com/jsdemo.htm
參考文章
https://www.cnblogs.com/googlegis/p/14640897.html
https://lbsyun.baidu.com/index.php?title=jspopular3.0/guide/helloworld
https://zhuanlan.zhihu.com/p/506032108
到此這篇關(guān)于Vue集成百度地圖實(shí)現(xiàn)位置選擇的文章就介紹到這了,更多相關(guān)vue百度地圖位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue-router的Import異步加載模塊問題的解決方案
這篇文章主要介紹了詳解vue-router的Import異步加載模塊問題的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05vue3 座位選座矩陣布局的實(shí)現(xiàn)方法(可點(diǎn)擊選中拖拽調(diào)換位置)
由于公司項(xiàng)目需求需要做一個線上設(shè)置考場相關(guān)的座位布局用于給學(xué)生考機(jī)排號考試,實(shí)現(xiàn)教室考場座位布局的矩陣布局,可點(diǎn)擊選中標(biāo)記是否有座無座拖拽調(diào)換位置橫向縱向排列,本文給大家分享實(shí)現(xiàn)代碼,一起看看吧2023-11-11Echarts實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸(實(shí)例代碼)
這篇文章主要介紹了Echarts 如何實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸,通過動圖給大家展示效果,實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11Vue 項(xiàng)目部署到服務(wù)器的問題解決方法
本篇文章主要介紹了Vue 項(xiàng)目部署到服務(wù)器的問題解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)的實(shí)例詳解(params/query)
在使用`router.push`進(jìn)行路由跳轉(zhuǎn)到另一個組件時(shí),可以通過`params`或`query`來傳遞參數(shù),這篇文章主要介紹了vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09vue簡單實(shí)現(xiàn)購物車結(jié)算功能
這篇文章主要為大家詳細(xì)介紹了vue簡單實(shí)現(xiàn)購物車結(jié)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04