Created
April 27, 2015 20:58
-
-
Save ToeJamson/accab1ff8849d64c24fb to your computer and use it in GitHub Desktop.
Displaying Live Location Points & Tracks with JavaScript & MapBox and PubNub
This file contains 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
<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' /> |
This file contains 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 marker = L.marker([lat, lng], { | |
icon: L.mapbox.marker.icon({ | |
'marker-color': '#f86767' | |
}) | |
}); | |
marker.addTo(map); |
This file contains 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
message: function(message, channel) { | |
console.log(message) | |
lat = message['lat']; | |
lng = message['lng']; | |
map.setView([lat, lng]); | |
map_line.addLatLng([lat,lng]); | |
}, |
This file contains 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 map_line; |
This file contains 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
map_line = L.polyline([], {color: 'blue'}).addTo(map); |
This file contains 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
message: function(message, channel) { | |
console.log(message) | |
lat = message['lat']; | |
lng = message['lng']; | |
map_line.addLatLng([lat,lng]); | |
} |
This file contains 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 lat = null; | |
var lng = null; |
This file contains 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
if (navigator.geolocation) { | |
... | |
} |
This file contains 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
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 | |
} | |
); |
This file contains 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
function pubs() { | |
... | |
} |
This file contains 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
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")} | |
}) |
This file contains 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
L.mapbox.accessToken = <your token goes here> |
This file contains 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 map = L.mapbox.map('map', '<your map goes here>'); |
This file contains 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
map.setView([lat, lng], 17); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment