【问题标题】:Display information on Google Maps marker在谷歌地图标记上显示信息
【发布时间】:2019-05-17 16:33:40
【问题描述】:

我正在尝试获取保存在 firebase 实时数据库中的信息,并使用 Google Maps API 在地图上显示它们。

这是我的火力基地

这是我在地图上的标记

现在它打开并显示用户当前位置,并使用 Geofire 将纬度和经度坐标同步到 Firebase 实时数据库。

现在我可以从 firebase 检索信息并将其显示在地图上,但如果我添加另一个用户,信息仍然相同。为什么?

这是我目前的代码:

// This Function will  add/display that marker on the map
function AddDriver(data) {


  var LatLng = new 
    google.maps.LatLng(parseFloat(data.val().lat),parseFloat(data.val().lng 
    ));
  var bounds = new google.maps.LatLngBounds();

  var marker = new google.maps.Marker({
    //title:service,
    position:LatLng,
    map: map
  });



  // Attach it to the marker we've just added
  google.maps.event.addListener(marker, 'click', function() {
    getData();
  });

  markers[data.key] = marker; // add marker in the markers 
  array...
    document.getElementById("loc").innerHTML = loc_count;


  function getData(){
    var locationsRef = 
      firebase.database().ref('Locations/oEyL4QXg1XYAABIBGPvqYbzlNiv2');

    console.log(locationsRef);
    locationsRef.once('value', function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
        var childKey = childSnapshot.key;
        var childData = childSnapshot.val();
        console.log(childData);
        var contentString = childData;
        var infowindow = new google.maps.InfoWindow({
          content: contentString
        });
        infowindow.open(map,marker);

      });
    });
  }

}

【问题讨论】:

    标签: javascript android firebase google-maps firebase-realtime-database


    【解决方案1】:

    根据Firebase Document

    您正在使用locationsRef.once,它只会通知您一次数据更改

    我认为您必须使用 ValueEventListener 之类的东西,它会在发生任何更改时通知您 在你的快照中

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2014-10-15
      • 2013-09-13
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多