Created
April 27, 2015 21:29
-
-
Save ToeJamson/381514cf9843e9046307 to your computer and use it in GitHub Desktop.
Displaying Live Location Points & Tracks with JavaScript & Google Maps 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> | |
<link rel="stylesheet" | |
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> |
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 = new google.maps.Map(document.getElementById('map-canvas'), { | |
zoom: 15, | |
center: {lat: lat, lng : lng, alt: 0} | |
}); | |
map_marker = new google.maps.Marker({position: {lat: lat, lng: lng}, map: map}); | |
map_marker.setMap(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
function redraw() { | |
map.setCenter({lat: lat, lng : lng, alt: 0}) | |
map_marker.setPosition({lat: lat, lng : lng, alt: 0}); | |
} |
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']; | |
//custom method | |
redraw(); | |
} |
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; | |
var map_marker; | |
var lat = null; | |
var lng = null; | |
var lineCoordinatesArray = []; |
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
pushCoordToArray(lat, lng); | |
var lineCoordinatesPath = new google.maps.Polyline({ | |
path: lineCoordinatesArray, | |
geodesic: true, | |
strokeColor: '#2E10FF', | |
strokeOpacity: 1.0, | |
strokeWeight: 2 | |
}); | |
lineCoordinatesPath.setMap(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
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> |
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; | |
// sets your location as default | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
var locationMarker = null; | |
if (locationMarker){ | |
// return if there is a locationMarker bug | |
return; | |
} | |
lat = position.coords["latitude"]; | |
lng = position.coords["longitude"]; | |
console.log(lat, lng); | |
}, | |
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
<style> | |
html, body, #map-canvas { | |
height: 700px; | |
margin: 0px; | |
padding: 0px | |
} | |
</style> |
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
<div id="map-canvas"></div> |
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' | |
}) |
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.subscribe({ | |
channel: "mymaps", | |
message: function(message, channel) { | |
console.log(message) | |
lat = message['lat']; | |
lng = message['lng']; | |
//custom method | |
redraw(); | |
}, | |
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
pubs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment