Firebase - 遵循公交数据集教程?

时间:2014-08-04 20:05:53

标签: firebase

我在Firebase上很新,我试图在Firebase的文档中使用本教程: https://www.firebase.com/docs/open-data/transit.html

但我真的被卡住了......或者更好,我完全迷失了。屏幕上没有任何内容。

<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script src="https://cdn.firebase.com/js/client/1.0.18/firebase.js"></script>
<script src="https://www.firebase.com/resources/js/publicdata/transit.js"></script>
    <script>
    // Lookup all buses on line "X".
    var transitLine = "5";
    var transitRef = new Firebase("https://publicdata-transit.firebaseio.com/ttc");
    var lineIndex = transitRef.child("routes").child(transitLine);

    lineIndex.on("child_added", function (snapshot) {
        var id = snapshot.name();
        transitRef.child("routes").child(id).on("vehicles", busUpdated);
    });
    lineIndex.on("child_removed", function (snapshot) {
        var id = snapshot.name();
        transitRef.child("routes").child(id).off("vehicles", busUpdated);
    });

    function busUpdated(snapshot) {
        // Bus line "X" changed location.
        var info = snapshot.val();
        // Retrieve latitude/longitude with info.lat/info.lon.
    }
</script>
</head>

<body>
<div id="map_canvas" style="position:relative;width:600px;height:400px; border: 1px solid #ccc; margin: 0 auto;"></div>
</body>

有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

原始剧本中有两个错误:

  1. 您正在观看错误的顶级参考(transit而不是routes
  2. 您正在收听不存在的活动(vehicles而不是value
  3. 此代码应该更好用:

    // Lookup all buses on line "X".
    var transitLine = "5";
    var transitRef = new Firebase("https://publicdata-transit.firebaseio.com/ttc");
    var lineIndex = transitRef.child("routes").child(transitLine);
    
    lineIndex.on("child_added", function (snapshot) {
        var id = snapshot.name();
        transitRef.child("vehicles").child(id).on("value", busUpdated);
    });
    lineIndex.on("child_removed", function (snapshot) {
        var id = snapshot.name();
        transitRef.child("vehicles").child(id).off("value", busUpdated);
    });
    
    function busUpdated(snapshot) {
        // Bus line "X" changed location.
        var info = snapshot.val();
        console.log(info);
        // Retrieve latitude/longitude with info.lat/info.lon.
    }
    

    小提琴:http://jsfiddle.net/tr5P7/