geolocation-stream is a node module that lets you use the HTML5 Geolocation API to watch location changes the node way -- with streams! It's designed for use with browserify.
npm install geolocation-stream --save
In this example, movement
is a readable stream that speaks stream events: data
, error
and end
. That means you can pipe movement output to anything that accepts streams, such as an XHR. in this case the data
events will be lat/lon position updates using the watchPosition
part of HTML5 geolocation.
var movement = require('geolocation-stream')()
movement.on('data', function(data) {
console.log(data)
})
movement.on('error', function(err) {
console.error(err)
})
Bundle it up into a browser-friendly file:
npm install browserify -g
browserify src.js -o bundle.js
Your browser's Geolocation API may not work
with the file://
protocol, so you can't just pop open demo/index.html
in your browser.
Instead you'll need to run an HTTP server locally. Luckily that's not so hard:
cd geolocation-stream
npm install
npm start
Open localhost:9966 in your browser and open the Javascript console. Behold your geolocation stream! It should look something like this:
{
timestamp:1383203179029,
coords:{
speed:null,
heading:null,
altitudeAccuracy:null,
accuracy:28,
altitude:null,
longitude:-122.4236727,
latitude:37.752089
}
}
MIT