【发布时间】:2020-08-09 03:58:46
【问题描述】:
我正在尝试使用 Leaflet 地图和 firebase 数据库来检索坐标来构建 Angular 项目。一切正常。除了一件事。当我更新标记位置或从 firebase 数据库中删除时。标记没有更新。我必须重新加载页面才能获得新的标记位置。
首页TS:
export class Tab1Page implements OnInit {
map: Map;
comingData: AngularFireList<any>;
constructor(public af: AngularFireAuth,public db: AngularFireDatabase) { }
ngOnInit(){
this.leafletMap();
}
// retrieve data from DB and init map
async leafletMap() {
this.comingData = this.db.list("/towuser");
this.comingData.snapshotChanges()
.pipe(
map(changes =>
changes.map(c => ({ $key: c.payload.key, ...c.payload.val() }))
)
).subscribe((a: any) => {
console.log(a)
if (a) {
// init map
this.map = new Map('mapId').setView([33, 44], 7);
tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
}).addTo(this.map);
// marker icon
var myIcon = icon({
iconUrl: '../../assets/icon/truck.png',
iconSize: [100, 35],
});
a.forEach(po => {
if (po.pos) {
// init marker position from database
const nantes = marker([po.pos.loc.lng,po.pos.loc.long], { icon: myIcon, }).addTo(this.map)
// marker Popup
const customPopup = `
<div style=" direction: rtl; text-align: right; ">
<div style=" direction: rtl; text-align: right; ">
<span style=" color: #42d77d; font-size: initial; ">
<strong>نوع الكرين : </strong></span>
<span style="font-size: initial; ">${po.profile.type}</span>
</div>
`
const customOptions = { 'className': 'custom-popup' }
nantes.bindPopup(customPopup, customOptions).on('click', function (e) { this.openPopup() });
nantes.setLatLng([po.pos.loc.lng,po.pos.loc.long]);
}
});
}
})
}
}
如何在不重新加载页面的情况下更新标记位置或从 firebase 数据库中删除?
【问题讨论】:
标签: javascript angular firebase-realtime-database leaflet