【问题标题】:How to convert data from firebase to an html array如何将数据从firebase转换为html数组
【发布时间】:2017-05-19 19:23:48
【问题描述】:

我想用从我的 firebase 数据库中检索到的数据替换这个数据(html 数组)。

    var locations = [
    ['Bondi Beach', -33.890542, 151.274856, 4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]

这是数据库的样子

I want to put parkers on the location and set the other attributes as descriptions

有人可以帮我如何将我的数据从 firebase 转换为 html 数组,如上面的代码。

为什么在更新数据库时使用 .on 而不是 .once 不会更新表。

    rootRef.on("child_added", snap =>{
    var date = snap.child("dateAndTime").val();
    var lat = snap.child("latitude").val();
    var long = snap.child("longitude").val();
    var link = snap.child("link").val();
    var report = snap.child("report").val();
    var status = snap.child("status").val();
    var needs = snap.child("needs").val();


    $("#table_body").append("<tr><td>" +date+"</td><td>"+report+"</td><td>"+lat+"</td><td>"+long+"</td><td>"+status+"</td><td>"+needs+"</tr>");

});

【问题讨论】:

  • 那是安卓的。我的问题是关于网络的。
  • 真的吗?您已删除所有编写的代码。为什么?
  • 我已经弄清楚如何处理其余代码。现在唯一的问题是将数据转换为上面的 html 数组

标签: html firebase web firebase-realtime-database


【解决方案1】:

从 Firebase 获取

我假设 firebase 设置正确

// Default markers
var locations = [{
    report: "Bondi Beach",
    longitude: -33.890542,
    latitude: 151.274856
}, {
    report: "Maroubra Beach",
    longitude: -33.950598,
    latitude: 151.274856
}];

 var colors = [
   "http://maps.google.com/mapfiles/ms/icons/red-dot.png",
   "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
 ]


var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: new google.maps.LatLng(-33.92, 151.25),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();
var markers = []

// To dynamically change marker color, use marker[i].setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')

function renderMarkers() {

    // Clearout old markers
    markers.map(function (oldMarker) {
        oldMarker.setMap(null)
    })
    markers = []

    locations.map(function (loc) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(loc.longitude, loc.latitude),
            map: map,
            icon: loc.completed ? colors[0] : colors[1]
        });
        markers.push(marker)
        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(loc.report);
                infowindow.open(map, marker);
            }
        })(marker, loc));
    })
}
renderMarkers()


firebase.database.ref('/database/5-11-2017').on('value', function (snapshot) {
    var result = snapshot.val()

    if (!result) return

    // I dont know you database, either it will return objects or array.
    locations = Object.keys(result).map(function (key) {
        return result[key]
    })
    // or
    // locations = result // Array

    // Render makers with new locations
    renderMarkers()

})

Example 为了模拟数据的加载,我添加了超时功能。地图应该会在大约 5 秒后呈现新的标记。

【讨论】:

  • Kumar,我的数据库是带有描述“我想放置标记......”的链接。实际上我只是想从数据库中检索数据到上面的一个 html 数组中
  • @Est 所以它不做吗?上面的代码应该可以完成这项工作。不是,你必须阅读文档
  • 谢谢,我让我工作了。谢谢你的帮助。我能再问你一个问题吗?是否可以使用不同颜色的标记?例如,在我的数据库中,有一个正在进行的事件和那些已经完成的事件。我可以放置显示不同颜色的标记吗?例子。红色表示正在进行,绿色表示已完成
  • @Priyesh 谢谢伙计,是否可以在一段时间内刷新标记?像每1分钟?因为我正在从实时数据库中获取位置,并且我想更新有关其状态的所有标记(尚未响应,正在进行,已完成)。 TIA
  • Firebase 有一个实时数据库。所以每当数据发生变化时,上面的函数都会自动调用,不需要轮询。更新数据库所需的一切
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2021-05-06
  • 2017-02-24
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多