Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Created April 27, 2015 20:58
Show Gist options
  • Save ToeJamson/accab1ff8849d64c24fb to your computer and use it in GitHub Desktop.
Save ToeJamson/accab1ff8849d64c24fb to your computer and use it in GitHub Desktop.
Displaying Live Location Points & Tracks with JavaScript & MapBox and PubNub
<script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css' rel='stylesheet' />
var marker = L.marker([lat, lng], {
icon: L.mapbox.marker.icon({
'marker-color': '#f86767'
})
});
marker.addTo(map);
message: function(message, channel) {
console.log(message)
lat = message['lat'];
lng = message['lng'];
map.setView([lat, lng]);
map_line.addLatLng([lat,lng]);
},
var map_line;
map_line = L.polyline([], {color: 'blue'}).addTo(map);
message: function(message, channel) {
console.log(message)
lat = message['lat'];
lng = message['lng'];
map_line.addLatLng([lat,lng]);
}
var lat = null;
var lng = null;
if (navigator.geolocation) {
...
}
navigator.geolocation.getCurrentPosition(function(position) {
var locationMarker = null;
if (locationMarker){
// return if there is a locationMarker bug
return;
}
// sets default position to your position
lat = position.coords["latitude"];
lng = position.coords["longitude"];
},
function(error) {
console.log("Error: ", error);
},
{
enableHighAccuracy: true
}
);
function pubs() {
...
}
pubnub = PUBNUB.init({
publish_key: 'publish_key',
subscribe_key: 'subscribe_key'
})
pubnub.subscribe({
channel: "mymaps",
message: function(message, channel) {
console.log(message)
lat = message['lat'];
lng = message['lng'];
},
connect: function() {console.log("pubnub connected")}
})
L.mapbox.accessToken = <your token goes here>
var map = L.mapbox.map('map', '<your map goes here>');
map.setView([lat, lng], 17);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment