Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Last active May 11, 2018 21:50
Show Gist options
  • Save ToeJamson/4cc8bb21b7e6606240ab to your computer and use it in GitHub Desktop.
Save ToeJamson/4cc8bb21b7e6606240ab to your computer and use it in GitHub Desktop.
Receiving and Displaying Vehicle Data with Automatic and MapBox
var fs = require('fs');
var coffee = require('coffee-script');
main = ->
L.mapbox.accessToken = '<your-mapbox-api-key>'
map = L.mapbox.map('map', '<your-map-code>')
map.setView [lat, lng], 5
marker = L.marker [lat, lng],
icon: L.mapbox.marker.icon('marker-color': '#f86767')
marker.addTo map
if navigator.geolocation
navigator.geolocation.getCurrentPosition (position) ->
if locationMarker?
# return if there is a locationMarker bug
return
# sets default position to your position
lat = position.coords['latitude']
lng = position.coords['longitude']
pubs()
main()
(error) ->
console.log 'Error: ', error,
enableHighAccuracy: true
app.get('/app.css', function(req, res) {
res.header('Content-Type', 'text/css');
file = fs.readFileSync(__dirname + '/client/styles/css/app.css', 'ascii');
res.send(file);
})
app.get('/:script.js', function(req, res) {
res.header('Content-Type', 'application/x-javascript');
cs = fs.readFileSync(__dirname + '/client/js/' +
req.params.script + '.coffee', 'ascii');
js = coffee.compile(cs);
res.send(js);
})
title= title
script(src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js")
script(src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js")
...
body
block content
script(src="/map_box_example.js")
extends layout_template
block content
.container
h1 Application online
a.btn.btn-primary(href="/login").
Login
extends layout_template
block content
.container
h1 MapBox Example
div#map
html, body, #map {
height: 700px;
margin: 0px;
padding: 0px
}
lat = null
lng = null
map = undefined
marker = undefined
notificationType = undefined
vehicle = undefined
pubs = ->
pubnub = PUBNUB.init(
publish_key: 'publish_key_here'
subscribe_key: 'subscribe_key_here')
pubnub.subscribe
channel: 'automaticChannel'
message: (message, channel) ->
console.log message
lat = message.location.lat
lng = message.location.lon
notificationType = message.type
vehicle = message.vehicle.display_name
map.setView [lat,lng]
marker.setLatLng L.latLng(lat, lng)
marker.bindPopup("<p>#{notificationType}</p>" +
"<p>Vehicle: #{vehicle}</p>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment