生活随笔
收集整理的這篇文章主要介紹了
vue获取地图经纬度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該功能只需要獲取經緯度(別忘了npm百度插件)
首先需要一個js,這邊我定義為map.js
在你需要的頁面import
在created里加載
獲取經緯度代碼
map.js
export function loadBMap
(funcName
) {var script
= document.createElement
("script");script.src
= "http://api.map.baidu.com/api?v=2.0&ak=你的秘鑰&callback=" + funcName
;document.body.appendChild
(script
);
}
import { loadBMap
} from
"./../../../static/js/map";
created
() {window.initBaiduMapScript
= () => {// console.log
(BMap
);this.getlocation
();};loadBMap
("initBaiduMapScript");},
// 獲取經緯度getlocation
() {const that
= this
;this.
$nextTick(function
() {try
{const geolocation
= new BMap.Geolocation
();geolocation.getCurrentPosition
(function
(r
) {// console.log
(r
);this.lat
= r.latitude
;this.lng
= r.longitude
;this.city
= r.address.city
;localStorage.setItem
("lat", r.latitude
);localStorage.setItem
("lng", r.longitude
);localStorage.setItem
("city", r.address.city
);if (this.getStatus
() == BMAP_STATUS_SUCCESS
) {const
{ lat
= null, lng
= null
} = r.point
;}});} catch
(e
) {console.log
(e
);}});}
總結
以上是生活随笔為你收集整理的vue获取地图经纬度的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。