1
votes

I'm pretty new on Firebase, I'm trying to follow this tutorial in Firebase's documentation: https://www.firebase.com/docs/open-data/transit.html

But I am really stuck.. Or better, I am completly lost. Nothing appears on the screen.

<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>

Someone knows what am I doing wrong?

1
If you go to this URL (publicdata-transit.firebaseio.com/ttc), you will see that there is no child named transit. You probably are looking for routes. var lineIndex = transitRef.child("routes").child(transitLine);Frank van Puffelen
I see. Thanks for the help! Unfortunately, this issue still remains.h1ghland3r
You register for an event with on("vehicles". I've never seen an event called vehicles and doubt it exists. Do you mean child("vehicles").on("child_added"?Frank van Puffelen
Fiddle: jsfiddle.net/tr5P7 (outputs to JavaScript console)Frank van Puffelen

1 Answers

0
votes

There were two mistakes in your original script:

  1. You are watching the wrong top-level ref (transit instead of routes)
  2. You are listening for a non-existing event (vehicles instead of value)

This code should work better:

// 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.
}

A fiddle with it: http://jsfiddle.net/tr5P7/