<baidu-map
:center="center"
:zoom="zoom"
:scroll-wheel-zoom="true"
@ready="handler"
@click="mapClick"
style="width:85%;height: 300px;margin-left:50px;"
:map-click="false"
@mousemove="syncPolygon"
@rightclick="newPolygon"
>
<bm-polygon
:path="path"
v-for="path of polygonPath.paths"
:key="path.toString()"
stroke-color="#529aff"
fill-color="#dfebff"
:fill-opacity="0.8"
:stroke-opacity="0.5"
:stroke-weight="2"
@click="alertpath"
/>
</baidu-map>
import { BaiduMap, BmNavigation, BmView, BmGeolocation, BmCityList } from 'vue-baidu-map'
export default {
name: 'AddGridPersonnel',
components: {
BaiduMap,
BmNavigation,
BmView,
BmGeolocation,
BmCityList
},
data () {
return {
// 地圖信息
center: {
lng: 114.037548,
lat: 22.616857
},
zoom: 18,
locData: {
longitude: '114.037548',
latitude: '22.616857',
address: ''
},
polygonPath: {
editing: false,
paths: [] // 繪制完成后的經(jīng)緯度,其實(shí)是在畫的時(shí)候動態(tài)push的,因?yàn)樵邳c(diǎn)擊的時(shí)候觸發(fā)了 paintPolygon 函數(shù)
}
}
},
// 方法
methods: {
// 開啟多邊形繪制
toggle (name) {
this[name].editing = !this[name].editing
// 在這里做一步判斷,如果有路徑且開啟繪制就把原來的路徑清空
if (this.polygonPath.paths && this.polygonPath.editing) {
this.polygonPath.paths = []
}
},
// 鼠標(biāo)移動時(shí)
syncPolygon (e) {
if (!this.polygonPath.editing) {
return
}
const { paths } = this.polygonPath
if (!paths.length) {
return
}
const path = paths[paths.length - 1]
if (!path.length) {
return
}
if (path.length === 1) {
path.push(e.point)
}
this.$set(path, path.length - 1, e.point)
},
// 鼠標(biāo)左鍵點(diǎn)擊時(shí)往路徑里push一個(gè)點(diǎn)
newPolygon (e) {
if (!this.polygonPath.editing) {
return
}
// 當(dāng)開始繪制后把按鈕調(diào)回開始繪制狀態(tài),防止繪制多個(gè)圖形
this['polygonPath'].editing = !this['polygonPath'].editing
const { paths } = this.polygonPath
if (!paths.length) {
paths.push([])
}
const path = paths[paths.length - 1]
path.pop()
if (path.length) {
paths.push([])
}
},
// 鼠標(biāo)右鍵多邊形繪制完成
paintPolygon (e) {
if (!this.polygonPath.editing) {
return
}
const { paths } = this.polygonPath
!paths.length && paths.push([])
paths[paths.length - 1].push(e.point)
},
alertpath (e) {
console.log(e.currentTarget.so)
console.log(this.polygonPath.paths[0])
},
handler ({ BMap, map }) {
console.info('BMap ', BMap)
window.map = map
this.addMarker(this.center.lng, this.center.lat)
},
// 點(diǎn)擊地圖
mapClick (e) {
console.log('繪制', this.polygonPath.editing)
if (this.polygonPath.editing) {
this.paintPolygon(e)
} else {
this.clickEvent(e)
}
},
// 點(diǎn)擊地圖監(jiān)聽
clickEvent (e) {
this.addMarker(e.point.lng, e.point.lat)
// 用所定位的經(jīng)緯度查找所在地省市街道等信息
var point = new window.BMap.Point(e.point.lng, e.point.lat)
var gc = new window.BMap.Geocoder()
const _this = this
gc.getLocation(point, function (rs) {
// var addComp = rs.addressComponents
console.log(rs.address) // 地址信息
_this.locData.address = rs.address
_this.form.setFieldsValue({
longitude: e.point.lng,
latitude: e.point.lat,
address: rs.address
})
})
this.locData.longitude = e.point.lng
this.locData.latitude = e.point.lat
},
addMarker (lng, lat) {
this.$nextTick(() => {
console.log('addMarker', lng, lat)
if (window.map) {
window.map.clearOverlays()
console.log('window.map', window.map)
const iconDemo = new window.BMap.Icon(
require('@/assets/icons/marker_red_sprite.png'),
new window.BMap.Size(64, 64),
{ anchor: new window.BMap.Size(18, 32), imageSize: new window.BMap.Size(36, 36) }
)
var myMarker = new window.BMap.Marker(new window.BMap.Point(lng, lat), { icon: iconDemo })
console.log('window.map', window.map)
window.map.addOverlay(myMarker)
console.log('window.map', window.map)
}
})
}, }
}
百度地圖圍欄+點(diǎn)擊獲取位置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- HTML JS ??????????java育兒園里的小學(xué)生“磨陀貨”友情提供?。?!??????????請大家尊重...
- CGPoint touchPoint = [gestureRecognizer locationInView:se...
- 需求:點(diǎn)擊按鈕喚起地圖,頁面沒滾動的時(shí)候,在地圖上點(diǎn)擊的位置就是鼠標(biāo)的實(shí)際點(diǎn)擊位置,但是如果頁面發(fā)生滾動后,點(diǎn)擊就...
- 項(xiàng)目要求:進(jìn)入頁面自動顯示到當(dāng)前位置,同時(shí)鼠標(biāo)點(diǎn)擊地圖獲取到點(diǎn)擊的地理位置 一、npm安裝 二、在main.js引...