MOD 12 - GEO LOCATION

by SHELDON PASCIAK

HTML

<!--
    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]);
       ...