Skip to content

Instantly share code, notes, and snippets.

@hyuki0000
Last active January 3, 2016 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki0000/8466930 to your computer and use it in GitHub Desktop.
Save hyuki0000/8466930 to your computer and use it in GitHub Desktop.

Where were you?

This tiny app ("Where were you?") saves your current position periodically in your browser's local storage.

  • This app is under construction.
  • The append button is for debug.
  • The clear button is to delete all records.
  • The register button register current position with a assigned name.

Plan

  • Save current position with your registered name ("My Office", "Starbucks", etc.)
  • Edit places and/or records.
  • Export records and places.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-icon" href="where.png" />
<title>Where were you?</title>
<script type="text/javascript" src="where.js"></script>
</head>
<body>
<h1>Where were you?</h1>
<input type="button" value="append" id="append" />
<input type="button" value="clear" id="clear" />
<h2>Register Place</h2>
<input type="text" value="Place A" id="place" /><input type="button" id="register" value="Register Place" /><br />
<h2>Places</h2>
<ul id="places"></ul>
<h2>Records</h2>
<ul id="records"></ul>
<h2>Debug</h2>
<pre id="debug"></pre>
</body>
</html>
var Where = {
// Utils
debug: function (s) {
document.getElementById("debug").innerHTML += "debug: " + s + "\n";
},
error: function (s) {
document.getElementById("debug").innerHTML += "ERROR: " + s + "\n";
},
// Events
onLoad: function() {
"use strict";
Where.debug('application started.');
if (navigator.geolocation) {
Where.debug('Geo Location API is supported.');
// navigator.geolocation.getCurrentPosition(success, error);
Where.watchid = navigator.geolocation.watchPosition(
Where.watchPosition,
Where.watchError,
Where.watchOption
);
Where.debug('watchPosition returns ' + Where.watchid);
} else {
Where.error('Geo Location API is not supported.');
}
document.getElementById("append").onclick = Where.onAppend;
document.getElementById("clear").onclick = Where.onClear;
document.getElementById("register").onclick = Where.onRegister;
},
onClear: function (e) {
Where.records = [];
Where.debug('Cleared.');
localStorage.setItem('records', JSON.stringify(Where.records));
Where.updateDisplay();
},
onAppend: function (e) {
var now = new Date();
Where.appendRecord({
getMilliseconds: now.getMilliseconds(),
toLocaleString: now.toLocaleString()
});
},
onRegister: function (e) {
if (Where.records === null) {
Where.error("onRegister: No records.");
} else if (Where.records.length == 0) {
Where.error("onRegister: No records.");
} else {
var name = document.getElementById("place").value;
var lat = Where.records[0].latitude;
var lon = Where.records[0].longitude;
Where.debug("onRegister: name = " + name);
Where.debug("onRegister: lat = " + lat);
Where.debug("onRegister: lon = " + lon);
Where.appendPlace({
name: name,
latitude: lat,
longitude: lon,
});
}
},
// View
updateDisplay: function () {
document.getElementById("records").innerHTML = Where.records.map(function(obj) {
return "<li>" + JSON.stringify(obj) + "</li>";
}).join('');
document.getElementById("places").innerHTML = Where.places.map(function(obj) {
return "<li>" + JSON.stringify(obj) + "</li>";
}).join('');
},
// Model
records: [],
places: [],
appendRecord: function (obj) {
Where.records = JSON.parse(localStorage.getItem('records'));
if (Where.records === null) {
Where.records = [];
}
Where.records.unshift(obj);
// Where.debug("appendRecord: " + JSON.stringify(obj));
localStorage.setItem('records', JSON.stringify(Where.records));
Where.updateDisplay();
},
appendPlace: function (obj) {
Where.places = JSON.parse(localStorage.getItem('places'));
if (Where.places === null) {
Where.places = [];
}
Where.places.unshift(obj);
Where.debug("appendPlace: " + JSON.stringify(obj));
localStorage.setItem('places', JSON.stringify(Where.places));
Where.updateDisplay();
},
// Geolocation
watchid: -1,
watchPosition: function (pos) {
var now = new Date();
var lat = pos.coords.latitude;
var lon = pos.coords.longitude;
Where.debug('(latitude, longitude) = (' + lat + ', ' + lon + ')');
Where.appendRecord({
time: now.getTime(),
latitude: lat,
longitude: lon,
timeISOString: now.toISOString()
});
},
watchError: function (error) {
Where.error('watchError: ' + error.code + ': ' + error.message);
switch (error.code) {
case 1:
alert('watchPosition: PERMISSION_DENIED');
break;
case 2:
alert('watchPosition: POSITION_UNAVAILABLE');
break;
case 3:
// alert('watchPosition: TIMEOUT');
break;
default:
alert('watchPosition: UNKNOWN_ERROR');
}
},
watchOption: {
enableHighAccuracy: false,
timeout: 3000,
maximumAge: 2000
},
terminator: null
};
window.onload = Where.onLoad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment