vue 百度地圖 + 定位
前提需要自己有百度的密鑰,如沒有可以去百度地圖申請
一、在主目錄下的index.html引入js,例如:
二、在webpack.base.conf.js配置文件中配置BMap,在module.exports 中與entry平級,例如:
三、在項目中引入BMap:
四、代碼:
<template> <div class="home"> <div id="allmap" class="allmap"></div> </div></template><script> import {getStore, setStore, removeStore} from '@/config/Utils' import BMap from 'BMap' export default { data () { return { type: 'tab', address_detail: null, center: {lng: 116.40387397, lat: 39.91488908} } }, mounted () { this.ready() }, methods: { ready () { let map = new BMap.Map('allmap') let point = new BMap.Point(this.center.lng, this.center.lat) map.centerAndZoom(point, 10) map.enableScrollWheelZoom(true) map.enableDoubleClickZoom(true) var geolocation = new BMap.Geolocation() geolocation.getCurrentPosition((r) => { if (r.point) { this.center.lng = r.longitude this.center.lat = r.latitude let markers = new BMap.Marker(r.point) map.addOverlay(markers) map.panTo(r.point) map.centerAndZoom(r.point, 16) } }, { enableHighAccuracy: true }) } } }</script><style> #allmap{ width: 100%; height: 15rem; }</style>
五、效果:
如有錯誤地方,請留言指教,謝謝大家
以上所述是小編給大家介紹的vue百度地圖 + 定位詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答