【问题标题】:Get data from collection in firestore从 Firestore 中的集合中获取数据
【发布时间】:2018-08-02 20:10:37
【问题描述】:

我将地理点存储到一个数组中,现在我想在谷歌地图上输出这些,但它不起作用。没有为新地理点设置标记。

需要帮助,如何在地图上为地理点设置标记?

firestore 数据库:

代码:

   mounted () {

    var map = new google.maps.Map(document.getElementById('mapName'), {
      center: {lat: 0, lng: 0},
      zoom: 20
    });


   fb.usersCollection.where("acctPosition", "==", true).get().then(docs => {
    docs.forEach((coord) => {
     const position = new google.maps.LatLng(coord.latitude,coord.longitude);
     const marker = new google.maps.Marker({
        position,
        map: map
       });
     })
    })
  }

这是console.log(coord.data())的输出

有了这个地理点,我想在谷歌地图中设置一个标记。如何做到这一点?

【问题讨论】:

    标签: javascript google-maps firebase vue.js google-cloud-firestore


    【解决方案1】:

    以下应该可以解决问题:

       fb.usersCollection.get().then(docs => {
        docs.forEach((coord) => {
         const lat = coord.data().acctPosition[0].lat;
         const lng = coord.data().acctPosition[0].lng;
         const position = new google.maps.LatLng(lat, lng);
         const marker = new google.maps.Marker({
            position,
            map: map
           });
         })
        })
    

    docs.forEach() 返回QueryDocumentSnapshot(“提供与 DocumentSnapshot 相同的 API 界面”)。因此,您必须调用 data() method 来获取文档字段。

    【讨论】:

    • 如果在forEach 循环中执行console.log(coord.data().latitude); 会得到什么?
    • 什么都没有,没有控制台日志
    • 如果你这样做docs.forEach((coord) => {console.log(coord) ...。只是想验证fb.usersCollection.where("acctPosition", "==", true).get() 真的返回了一些东西......
    • 控制台也没有输出
    • 是的,它现在正在工作。非常感谢您和您的耐心
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2018-07-13
    • 2020-11-26
    • 2021-10-31
    • 2021-06-28
    • 2020-06-03
    相关资源
    最近更新 更多