【发布时间】:2020-06-02 04:20:00
【问题描述】:
我正在尝试使用 Angular 和 angularfire 构建一个项目。我的项目是关于使用传单地图绘制多边形,然后将多边形的坐标推送到数据库。我已成功将数组推送到数据库,但是从数据库中检索坐标时出现问题。
数据库结构:
{
"Locations" : {
"-M0M9kqEbE0FVMzIZAg0" : {
"text" : [ [ {
"lat" : 35.97800618085566,
"lng" : 42.03369140625001
}, {
"lat" : 33.88865750124075,
"lng" : 41.46240234375001
} ] ]
}
}
}
我正在尝试将上面的数组存储在 ts 类而不是 html 中。
我的代码:
export class WeatherNowComponent {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
coords :any
constructor(private db: AngularFireDatabase) {
this.itemsRef = this.db.list('Locations')
// Use snapshotChanges().map() to store the key
this.items = this.itemsRef.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
);
this.db.list('Locations').snapshotChanges().subscribe((markers: any) => {
console.log(markers)
markers.forEach(singlemarker => {
console.log(singlemarker)
// ADD COORDS to MAP POLYGON
var polygon = L.polygon([singlemarker.text.lat,singlemarker.text.lng], {color: 'red'}).addTo(this.map);
});
});
console.log(this.items)
}
}
所以我在控制台日志中只有键数组,没有其他数组对象。
【问题讨论】:
-
"Locations" : {应该是"Locations" : [{?
标签: javascript arrays angular firebase-realtime-database angularfire2