Vue百度地圖maker標(biāo)注的添加和刪除方式
vue百度地圖maker標(biāo)注的添加和刪除
const lng = parseFloat(e.longitude)
const lat = parseFloat(e.latitude)
const point = new BMap.Point(lng, lat)
const marker = new BMap.Marker(point)
map.addOverlay(marker)
marker.setPosition(point)//添加標(biāo)注
//刪除標(biāo)注文檔上注明了是removeOverlay()這個(gè)方法,具體使用如下
marker.id = data[0].id//給marker添加id對(duì)應(yīng)數(shù)組里的id(非必要不要賦值id,不要這么寫)
const allOverlays = map.getOverlays()//返回地圖上所有的覆蓋物
//循環(huán)找到數(shù)組里面id和返回覆蓋物相同的
for (let i = 0; i < allOverlays.length; i++) {
if (allOverlays[i].id == data[0].id) { //data[0].id即是你要?jiǎng)h除標(biāo)注的id
map.removeOverlay(allOverlays[i])
}
}百度地圖API刪除指定的覆蓋物Marker
部分思路代碼
1.給地圖map添加覆蓋物Marker,注意給marker設(shè)定一個(gè)唯一表示,我這里用的是后端傳過來的id
const point = new BMap.Point(item.lng, item.lat) const marker = new BMap.Marker(point) marker.id = item.number this.map.addOverlay(marker)
2.根據(jù)getOverlays()方法獲取到地圖上所有的覆蓋物,并判斷覆蓋物的id是否和需要?jiǎng)h除的id一致,若一致則通過removeOverlay()刪除指定的覆蓋物
// 移除地圖覆蓋點(diǎn)
removeOverlay(lng, lat) {
const allOverlays = this.map.getOverlays()
for (let i = 0; i < allOverlays.length; i++) {
if (allOverlays[i].id && allOverlays[i].id === this.rowData.id) {
this.map.removeOverlay(allOverlays[i])
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用vue-recoure + http-proxy-middleware + vuex配合promise實(shí)現(xiàn)基本
這篇文章主要介紹了Vue使用vue-recoure + http-proxy-middleware + vuex配合promise實(shí)現(xiàn)基本的跨域請(qǐng)求封裝問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Vue3時(shí)間軸組件問題記錄(時(shí)間信息收集組件)
本文介紹了如何在Vue3項(xiàng)目中封裝一個(gè)時(shí)間信息收集組件,采用雙向綁定響應(yīng)式數(shù)據(jù),通過對(duì)Element-Plus的Slider組件二次封裝,實(shí)現(xiàn)時(shí)間軸功能,解決了小數(shù)計(jì)算導(dǎo)致匹配問題和v-model綁定組件無效問題,感興趣的朋友跟隨小編一起看看吧2024-09-09
Vue子組件內(nèi)的props對(duì)象參數(shù)配置方法
這篇文章主要介紹了?Vue?子組件內(nèi)的??props?對(duì)象里的?default?參數(shù)是如何定義Array、?Object?、或?Function?默認(rèn)值的正確寫法說明,感興趣的朋友跟隨小編一起看看吧2022-08-08
vue3中$attrs的變化與inheritAttrs的使用詳解
$attrs現(xiàn)在包括class和style屬性。?也就是說在vue3中$listeners不存在了,vue2中$listeners是單獨(dú)存在的,在vue3?$attrs包括class和style屬性,?vue2中?$attrs?不包含class和style屬性,這篇文章主要介紹了vue3中$attrs的變化與inheritAttrs的使用?,需要的朋友可以參考下2022-10-10

