Vue使用高德地圖選點定位搜索定位功能實現(xiàn)
項目下載:
高德地圖測試demo,高德地圖測試demo-Node.js文檔
效果圖:
一、申請高德地圖的使用密鑰key。
在高德地圖控制臺里面“我的應(yīng)用”中創(chuàng)建應(yīng)用,并添加key就可以拿到密鑰key和安全密鑰了。
二、安裝依賴。
npm install -S vue-amap
三、使用。
main.js引入依賴。
import VueAMap from 'vue-amap' Vue.use(VueAMap); // 初始化vue-amap VueAMap.initAMapApiLoader({ key: '你的key',// 高德的key // 插件集合 plugin: [ 'AMap.CircleEditor',// 圓形編輯器插件 "AMap.Geolocation", // 定位控件,用來獲取和展示用戶主機所在的經(jīng)緯度位置 "AMap.Geocoder", // 地理編碼與逆地理編碼服務(wù),用于地址描述與坐標間的相互轉(zhuǎn)換 "AMap.Autocomplete", "AMap.PlaceSearch", "AMap.CitySearch", ], // 高德 sdk 版本,默認為 1.4.4 v: '1.4.4' }); //高德的安全密鑰 window._AMapSecurityConfig = { securityJsCode:'你的安全密鑰', }
封裝組件:aliMap.vue
<template> <div> <div> <div style="border: 1px solid #cccccc"> <!-- //搜索框 --> <el-amap-search-box :search-option="searchOption" :on-search-result="onSearchResult" /> <!-- 地圖 --> <el-amap class="amap-box" :zoom="amap.zoom" :center="amap.center" :plugin="amap.plugin" :events="amap.events" > <!-- 當前位置圖標 --> <el-amap-marker v-for="(marker, index) in amap.markers" :key="'marker' + index" :position="marker.position" /> <!-- 位置名稱顯示 --> <el-amap-text v-for="(marker, index) in amap.markers" :key="'text' + index" :text="marker.text" :offset="marker.offset" :position="marker.position" /> </el-amap> </div> </div> </div> </template> <script> //初始數(shù)據(jù) function getFormData() { return { lat: "39.909186", lon: "116.39746", orgAddr: "天安門", province: "", city: "", district: "", }; } export default { name: "Map", data() { const vm = this; return { // form對象 dataForm: getFormData(), // 地圖搜索對象 searchOption: { city: "全國", citylimit: true, }, lng: 0, lat: 0, // 地圖對象 amap: { zoom: 16, center: [116.319802, 39.98294], events: { // 點擊獲取地址的數(shù)據(jù) click(e) { const { lng, lat } = e.lnglat; vm.dataForm.lon = lng; vm.dataForm.lat = lat; // 這里通過高德 SDK 完成。 var geocoder = new AMap.Geocoder({ radius: 100, extensions: "all", }); geocoder.getAddress([lng, lat], function (status, result) { if (status === "complete" && result.info === "OK") { if (result && result.regeocode) { console.log("點擊獲取地址的數(shù)據(jù)", result); vm.dataForm.orgAddr = result.regeocode.formattedAddress; vm.dataForm.province = result.regeocode.addressComponent.province; vm.dataForm.city = result.regeocode.addressComponent.city; vm.dataForm.district = result.regeocode.addressComponent.district; vm.dataForm.lat = lat ? lat.toString() : ""; vm.dataForm.lon = lng ? lng.toString() : ""; vm.amap.markers = []; const obj = { position: [lng, lat], text: result.regeocode.formattedAddress, offset: [0, 30], }; vm.amap.markers.push(obj); vm.sure(); } } }); vm.$nextTick(); }, }, plugin: [ { // 定位 pName: "Geolocation", events: { init(o) { // o是高德地圖定位插件實例 o.getCurrentPosition((status, result) => { console.log("定位:", result); if (result && result.position) { // 設(shè)置經(jīng)度 vm.lng = result.position.lng; // 設(shè)置維度 vm.lat = result.position.lat; // 設(shè)置坐標 vm.amap.center = [vm.lng, vm.lat]; let op = { position: [vm.lng, vm.lat], text: result.formattedAddress, offset: [0, 30], }; vm.amap.markers.push(op); // 頁面渲染好后 vm.$nextTick(); } }); }, }, }, ], // markers: [], }, }; }, created() {}, methods: { // 地圖搜索回調(diào) onSearchResult(pois) { const vm = this; vm.amap.markers = []; let latSum = 0; let lngSum = 0; console.log("地圖回調(diào)", pois); if (pois.length > 0) { pois.forEach((poi, index) => { const { lng, lat } = poi; if (index === 0) { lngSum = lng; latSum = lat; const obj = { position: [poi.lng, poi.lat], text: poi.name, offset: [0, 30], }; vm.amap.markers.push(obj); console.log("地圖搜索回調(diào)", poi); vm.dataForm.orgAddr = poi.name; vm.dataForm.lat = poi.lat ? poi.lat.toString() : ""; vm.dataForm.lon = poi.lng ? poi.lng.toString() : ""; } }); vm.amap.center = [lngSum, latSum]; } }, // 提交方法 sure() { const vm = this; this.$emit("updateLocation", vm.dataForm); }, }, }; </script> <style lang="scss" scoped> .amap-box { height: 44vh; height: calc(100vh - 45px); } .el-vue-search-box-container { // top: 45px; width: 100% !important; } .search-box { position: absolute; z-index: 5; width: 70%; left: 13%; top: 10px; height: 30px; } .search-box .el-input { float: left; width: 100%; height: 100%; box-sizing: border-box; border-radius: 0 7px 7px 0; outline: none; position: relative; } .search-box .el-button { position: absolute; right: 0; top: 1px; width: 20%; background: #38a28a; border-radius: 0 7px 7px 0; border: none; color: #fff; outline: none; } .tip-box { text-align: center; width: 100%; position: absolute; top: 35px; padding: 10px 0; background-color: #fff; opacity: 0.8; } </style>
使用組件:
<template> <div> <Map ref="map" @updateLocation="updateLocation" /> </div> </template> <script> import Map from '@/components/aliMap.vue' export default { name: '', props: { }, components: { Map }, data () { return { } }, mounted() { }, methods: { updateLocation(Addr) { console.log('位置信息:',Addr) } }, } </script> <style scoped lang='scss'> </style>
四、常見問題。
經(jīng)常有人說不顯示,麻煩自己檢查一下控制臺報錯。
1.css預(yù)處理器不一樣。
我的代碼用的是scss,使用時請根據(jù)自己項目使用的情況(scss或less,也可以將css預(yù)處理器刪除了)進行修改。
2.是否修改成自己的高德地圖key?
3.展示地圖的dom的容器是否存在大小,是否顯示?
4.高德地圖依賴是否一致?
第三方依賴經(jīng)常出現(xiàn)修改api的情況,如果控制臺報錯的是某個變量函數(shù)未定義或找不到,多半是依賴的版本不一致造成的,建議安裝指定版本。
5.部分網(wǎng)友還把百度地圖和高德地圖混入使用。
到此這篇關(guān)于Vue使用高德地圖選點定位搜索定位的文章就介紹到這了,更多相關(guān)vue高德地圖定位內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue源碼解析computed多次訪問會有死循環(huán)原理
這篇文章主要為大家介紹了vue源碼解析computed多次訪問會有死循環(huán)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04詳解vue2.0 資源文件assets和static的區(qū)別
這篇文章主要介紹了詳解vue2.0 資源文件assets和static的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖
這篇文章主要為大家詳細介紹了vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11詳解Vue改變數(shù)組中對象的屬性不重新渲染View的解決方案
這篇文章主要介紹了詳解Vue改變數(shù)組中對象的屬性不重新渲染View的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09關(guān)于vue狀態(tài)過渡transition不起作用的原因解決
這篇文章主要介紹了關(guān)于vue狀態(tài)過渡transition不起作用的原因解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題
今天小編就為大家分享一篇解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08