Created
March 12, 2015 02:27
-
-
Save bradymholt/4980ea0df5bcb4fd4000 to your computer and use it in GitHub Desktop.
Roadtrip to San Diego GPS Tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
var port = process.env.PORT || 1337; | |
var fs = require('fs'); | |
var url = require('url'); | |
var pathColor = '0x0000ff'; | |
var pathWeight = '5'; | |
var mapSize = '640x400'; | |
var houstonCoordinates = '29.73820459,-95.4696264'; | |
var sanDiegoCoordinates = '32.7153292, -117.1572551'; | |
var coor = { foo: [houstonCoordinates, sanDiegoCoordinates] }; | |
var startingMarker = 'color:blue%7Clabel:Houston%7C' + houstonCoordinates; | |
var endingMarker = 'color:green%7Clabel:San Diego%7C' + sanDiegoCoordinates; | |
var data = { coordinates : [houstonCoordinates], lastUpdated: new Date() }; | |
fs.writeFileSync('data.json', JSON.stringify(data)); | |
function getLastUpdatedDescription(){ | |
var now = new Date(); | |
var millisecondsDiff = (now - Date.parse(data.lastUpdated)); | |
if (millisecondsDiff > 60000) { | |
return parseInt(millisecondsDiff / 60000) + " minute(s) ago"; | |
} | |
else{ | |
return parseInt(millisecondsDiff / 1000) + " second(s) ago"; | |
} | |
return millisecondsDiff; | |
} | |
http.createServer(function(req, res) { | |
var urlParts = url.parse(req.url, true); | |
if (urlParts.query.coords !== undefined) { | |
data.coordinates.push(urlParts.query.coords); | |
data.lastUpdated = new Date(); | |
fs.writeFileSync('data.json', JSON.stringify(data)); | |
} | |
else { | |
var dataFile = fs.readFileSync('data.json'); | |
data = JSON.parse(dataFile); | |
var mapPath = 'color:red|weight:5|' + data.coordinates.join('|'); | |
var currentMarker = 'color:yellow%7Clabel:Current%7C' + data.coordinates[data.coordinates.length - 1]; | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.write('<!DOCTYPE html>\n<html lang="en-US">\n<head>\n</head>'); | |
res.write('\n<body>'); | |
res.write('\n<h1>Holt Roadtrip to San Diego</h2>'); | |
res.write('\n<h2>Track Our Progress!</h2>'); | |
res.write('\n<p>Last Update: ' + getLastUpdatedDescription() + '</p>'); | |
res.write('<img src="http://maps.googleapis.com/maps/api/staticmap?path=' + mapPath + '&size=' + mapSize + '&markers=' + startingMarker + '&markers=' + currentMarker + '&markers=' + endingMarker + '&sensor=false"/>'); | |
res.write('\n</body>\n</html>'); | |
} | |
res.end(); | |
}).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment