Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added date range [show only location history between two dates] #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>location-history-visualizer</title>
<link rel="stylesheet" href="index.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css"/>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<div class="visualizer">
Expand All @@ -21,6 +22,11 @@ <h2>location-history-visualizer</h2>
<p class="fallback">Alternatively, select your <b>LocationHistory.json</b> file directly: <input name="file" type="file" id="file"></input></p>
<hr>
<p><i>Experimental (even faster!) import method:</i> Instead of going through Google Takeout, simply browse to the <a href="https://maps.google.com/locationhistory/kml?startTime=0&endTime=9000000000000" target="_blank">Google Location History KML API endpoint</a>. A file (<b>history-12-31-1969.kml</b>) will be downloaded &ndash; just drag and drop it onto this page, and we're off! <i>Note:</i> This uses a non-public Google API and as such may cease to work at any point. Your mileage may vary.</p>
<hr>
<p>You can also view only the location history <b>between two dates</b>:<br/>
From: <input type="text" id="fromDate"> - To: <input type="text" id="toDate">
</p>
<hr>
<p class="credit">A project by <a href="https://theopolis.me" target="_blank">@theopolisme</a>. Made in 2014 in Memphis, Tennessee.</p>
</div>

Expand Down Expand Up @@ -88,6 +94,7 @@ <h2>Render complete!</h2>
</div>

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/3.8.4/dropzone.min.js"></script>
<script src="lib/leaflet.heat.min.js"></script>
Expand All @@ -103,6 +110,10 @@ <h2>Render complete!</h2>

ga('create', 'UA-55418641-1', 'auto');
ga('send', 'pageview');
$(function() {
$("#fromDate").datepicker();
$("#toDate").datepicker();
});
</script>
</body>
</html>
39 changes: 29 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@

// For mobile browsers, allow direct file selection as well
$( '#file' ).change( function () {
stageTwo( this.files[0] );
stageTwo( this.files[0], document.getElementById("fromDate").value, document.getElementById("toDate").value );
dropzone.disable();
} );
}

function stageTwo ( file ) {
function stageTwo ( file, fromDate, toDate ) {
heat = L.heatLayer( [], heatOptions ).addTo( map );

// First, change tabs
$( 'body' ).addClass( 'working' );
$( '#intro' ).addClass( 'hidden' );
$( '#working' ).removeClass( 'hidden' );


if(fromDate && toDate){
fromDate=new Date(document.getElementById("fromDate").value).getTime();
toDate=new Date(document.getElementById("toDate").value).getTime();
}

// Now start working!
processFile( file );

Expand All @@ -63,7 +68,15 @@
function getLocationDataFromJson ( data ) {
var SCALAR_E7 = 0.0000001, // Since Google Takeout stores latlngs as integers
locations = JSON.parse( data ).locations;


if(fromDate && toDate) {
i = locations.length;
while (i--) {
if (locations[i].timestampMs < fromDate || locations[i].timestampMs > toDate)
locations.splice(i, 1);
}
}

if ( !locations || locations.length === 0 ) {
throw new ReferenceError( 'No location data found.' );
}
Expand All @@ -77,18 +90,24 @@
var KML_DATA_REGEXP = /<when>(.*?)<\/when>\s*<gx:coord>(\S*)\s(\S*)\s(\S*)<\/gx:coord>/g,
locations = [],
match = KML_DATA_REGEXP.exec( data );

// match
// [1] ISO 8601 timestamp
// [2] longitude
// [3] latitude
// [4] altitude (not currently provided by Location History)

while ( match !== null ) {
locations.push( [ Number( match[3] ), Number( match[2] ) ] );
match = KML_DATA_REGEXP.exec( data );
if(fromDate && toDate) {
while ( match !== null ) {
if(new Date(match[1]).getTime() >= fromDate && new Date(match[1]).getTime() <= toDate)
locations.push( [ Number( match[3] ), Number( match[2] ) ] );
match = KML_DATA_REGEXP.exec( data );
}
}
else{
while ( match !== null ) {
locations.push( [ Number( match[3] ), Number( match[2] ) ] );
match = KML_DATA_REGEXP.exec( data );
}
}

return locations;
}

Expand Down