<!--
get and use geolocation - please see CONSOLE OUTPUT for additional information
Note: The ideal process would be the send the data on each visit to a central server.
Since I envision a future datastructure to be serializable and bound to a database, much of the thought on this problem refers to database implementation. I believe that's the intent of the data in the end. It isn't reasonable to consider a memory resident structure of millions of records.
please see console view for additional information
-->
<div id="demo"></div>
<input type="button" value="Visit" id="myButton" /><br />
User ID:
<input type="number" value=1 min=1 id="uid" value=1 />
<br />
<div id="visitRecord"></div>
JavaScript
var x = document.getElementById("demo");
/*
This object will contain the node data associated using the GPS coordinate as the key.
The Data within the node will be a list of each visit record with time stamp.
*/
var Locations = {};
var keys = [];
// Is this navigator.gelocation a separate thread call ? seems like it
// but I wouldn't really know one if I saw one
//http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var uid = $('#uid').val();
console.log("User ID: " + uid);
console.log(position.coords);
document.getElementById("visitRecord").innerHTML = "<br />User ID: " + uid + " <br />Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude + "<br />TimeStamp:" + Date.now() + "<br />";
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var ns = position.coords.latitude.toFixed(4);
var we = position.coords.longitude.toFixed(4);
console.log( "location key: [" + ns + " " + we +"]" );
if (Locations[ns+""+we]) {
Locations[ns+""+we].visits.push( "user: " + uid + ", " + "time:" + Date.now() );
} else
{
keys.push(ns+""+we);
Locations[ns+""+we]={};
Locations[ns+""+we].visits=[];
}
document.getElementById("visitRecord").innerHTML += "<br />" + JSON.stringify(Locations) + "<br />";
//note this is local to this browser
//this logic needs to be server level based on stored data from all users and all locations
//demonstrates that visits are put in bins using GPS location as the key
for (var i=0;i<keys.length;i++){
console.log(keys[i]);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.