Skip to content

Instantly share code, notes, and snippets.

@Khodl
Created January 13, 2016 20:42
Show Gist options
  • Save Khodl/98cea9c6c784767b434c to your computer and use it in GitHub Desktop.
Save Khodl/98cea9c6c784767b434c to your computer and use it in GitHub Desktop.
Show your NomadTrips location on your website
<div id="location">
<!-- Your location will be displayed there -->
</div>
<script>
/*
* Hey dear Digital Nomad!
* Feel free to copy that code ;-)
*
* @NathanaelKhodl
* */
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
treatData(JSON.parse(xmlhttp.responseText));
}
};
xmlhttp.open("GET", "https://nomadtrips.co/YOUR_USERNAME.json", true);
xmlhttp.send();
function treatData(data) {
if(! data || ! data.trips) return;
var city = false;
var now = new Date();
data.trips.forEach(function(c){
var dStart = new Date(c.date_start);
var dEnd = new Date(c.date_end);
if(dStart <= now && dEnd >= now)
city = c;
});
if(! city) return;
var location = [];
if(city.place)
location.push(city.place);
if(city.country)
location.push(city.country);
if(! location.length) return;
document.getElementById("location").innerHTML = "(I'm currently in "+ location.join(", ") +")";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment