-
-
Save stephenlb/051763f00e24a80364b42f2a1adde709 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="https://cdn.pubnub.com/sdk/javascript/pubnub.4.17.0.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 = new PubNub({ | |
publishKey : 'demo', | |
subscribeKey : 'demo' | |
}) | |
pubnub.subscribe({ | |
channels: ["mymaps"], | |
}); | |
pubnub.addListener({ | |
status: function(statusEvent) { | |
if (statusEvent.category === "PNConnectedCategory") { | |
console.log("pubnub connected"); | |
} | |
}, | |
message: function (message) { | |
console.log(message) | |
lat = message['lat']; | |
lng = message['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
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