如何使用rxjs / Observable从geopoint包含纬度和经度

时间:2018-02-08 19:23:38

标签: angular rxjs google-cloud-firestore

我假设Observeable映射了firestore db文档字段中的一行。如何不显示geopoint字段。以下代码显示正确提取纬度和经度,但我不知道如何让它们出现在网页上。

项目型号代码:

id?: string;  
lname?: string;
cname?: string;
authid?: string;
epc?: string;
latitude?: string;
longitude?: string; 

服务组件代码:

import { Injectable } from '@angular/core';
import { AngularFirestore, 
     AngularFirestoreCollection, 
     AngularFirestoreDocument } from 'angularfire2/firestore';
import * as firebase from 'firebase/app';
import { Observable } from'rxjs/Observable';
import { Item } from '../models/Item';

@Injectable()
export class ItemService {
    woodyplantsCollection: 
    AngularFirestoreCollection<Item>;
    woodyplants: Observable<Item[]>;
    itemDoc: AngularFirestoreDocument<Item>;

    constructor(public afs: AngularFirestore) { 
        this.woodyplantsCollection = 
        this.afs.collection('woodyplants', ref=> 
        ref.orderBy('lname', 'asc').limit(3));         
        this.woodyplants =        
        this.woodyplanttsCollection.
        snapshotChanges().map(changes => {
            return changes.map(a => {     
            const data = a.payload.doc.data() as Item;      
            data.id = a.payload.doc.id;   
            data.latitude = a.payload.doc.data().geopoint.latitude.toString();
            data.longitude = a.payload.doc.data().geopoint.longitude.toString();
            console.log("Latitude:",data.latitude);
            console.log("Longitude:", data.longitude);
            return data;     
            });
        });

1 个答案:

答案 0 :(得分:0)

尝试这种方式,您可以添加与您的模型匹配的键值对。

this.woodyplants = this.woodyplanttsCollection.snapshotChanges().map(changes => {
     return changes.map(a => {       
       const id = a.payload.doc.id;  
       const latitude = a.payload.doc.data().geopoint.latitude.toString();
       const longitude = a.payload.doc.data().geopoint.longitude.toString();

       return {id:id,latitude:latitude,longitude:longitude};    
   });
});