Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/1.html
Created November 15, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/b1ce5952bc3d550568f699bcb90b9b67 to your computer and use it in GitHub Desktop.
Save stephenlb/b1ce5952bc3d550568f699bcb90b9b67 to your computer and use it in GitHub Desktop.
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.17.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
that.json_data = file; // the original json object
that.formatted_json = formatJSON(that.json_data); // formatted json for simplicity
that.hash_table = extractHash(that.formatted_json); // coordinates encoded as hash strings
function formatJSON(json) {
var stations = json.root.stations[0].station;
var stations_formatted = [];
stations.forEach(function(station) {
var lat = station.gtfs_latitude[0];
var lng = station.gtfs_longitude[0];
var name = station.name[0];
var abbr = station.abbr[0];
new_station = {"lat": lat,"lng": lng,"name": name, "abbr": abbr};
stations_formatted.push(new_station);
});
return stations_formatted;
}
function extractHash(formatted_json) {
var hash_table = [];
formatted_json.forEach(function(station) {
geohash_string = geoToHash(station.lat,station.lng);
hash_table.push(geohash_string);
});
return hash_table;
}
function geoToHash(lat,lng) {
return ngeohash.encode(lat,lng,4);
}
function hashMatching(user_hash, hash_table) {
var matches = [];
hash_table.forEach(function(value,i) {
var value_hash = value.substring(0,6);
if (value_hash === user_hash) {
value_object = {"hash": value_hash, "index": i};
matches.push(value_object);
}
});
return matches;
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
that.lat = 37.6;
that.lng = -122.4;
var lat = that.lat;
var lng = that.lng;
var app = require("./app.js");
var json_data, hash_table, hash_matches, destinations;
json_data_formatted = app.formatted_json;
hash_table = app.hash_table;
hash_matches = app.hash_matches;
destinations = app.destinations;
pubs();
google.maps.event.addDomListener(window, 'load', initialize());
<script src="bundle.js"></script>
function pubs() {
pubnub = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
})
var business_marker = new google.maps.Marker({position: {lat: match_lat, lng: match_lng}, map: map});
business_marker.setMap(map);
google.maps.event.addListener(business_marker, 'mouseover', function() {
infowindow.open(map,business_marker);
});
google.maps.event.addListener(business_marker, 'mouseout', function() {
infowindow.close(map,business_marker);
$.ajax({
type: "GET",
url: "data/MLBR.json",
dataType: "json",
success: function(file) {
main_two(file);
}
});
|- station
|- Object
|- etd
|- Object (Destinations)
|- estimate
|- trains
|- minutes until departure, or “Leaving”
var etd = json.root.station[0].etd;
that.destinations = [];
var times = [];
etd.forEach(function(destination) {
var abbr = destination.abbreviation[0];
var dest = destination.destination[0];
var est = destination.estimate;
est.forEach(function(estimate) {
var min = estimate.minutes[0];
times.push(min);
});
that.destinations.push({"destination": dest, "abbreviation": abbr, "times": times});
});
var times = destination.times;
dest_string = dest_string + "<p><i>" + destination.abbreviation + ":" + "</i><p>";
times.forEach(function(time) {
dest_string = dest_string + " " + time;
});
var infowindow = new google.maps.InfoWindow({
content: "<b>" + BusinessName + "</b>" +
"<p>" + dest_string + "</p></p></p>"
});
browserify js/app.js -o bundle.js
var xml2js = require('xml2js'),
fs = require('fs'),
path = require('path'),
request = require('request');
request("http://api.bart.gov/api/stn.aspx?cmd=stns", function(error, response, body) {
// do something with response body here
});
function parseData(data) {
...
}
var parseString = xml2js.parseString;
parseString(data, function(err, result) {
string_result = JSON.stringify(result);
}
fs.writeFile(path.join(__dirname, '../','data/allstations.json'), string_result, function(err) {
$(document).ready(function() {
$.ajax({
type: "GET",
url: "data/allstations.json",
dataType: "json",
success: function(file) {
main(file);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment