var Fse; if( !Fse ) Fse = {}; if( ! Fse.Mobile ) Fse.Mobile = {}; Fse.Mobile.getCookie = function( sName, sDefault ) { var sRE = sName + "=([^;]*);?"; var oRE = new RegExp( sRE ); if( oRE.test( document.cookie ) ){ return decodeURIComponent( RegExp["$1"] ); } else { return sDefault; } }; Fse.Mobile.setCookie = function( sName, sValue, oExpires, sPath, sDomain, bSecure ) { var sCookie = sName + "=" + encodeURIComponent( sValue ); if( oExpires ) { sCookie += "; expires=" + oExpires.toGMTString(); } if( sPath ) { sCookie += "; path=" + sPath; } if( sDomain ) { sCookie += "; domain=" + sDomain; } if( bSecure ) { sCookie += "; secure"; } document.cookie = sCookie; }; var geocoder; // Check to see if this browser supports geolocation. if (navigator.geolocation) { // This is the location marker that we will be using // on the map. Let's store a reference to it here so // that it can be updated in several places. var locationMarker = null; // Get the location of the user's browser using the // native geolocation service. When we invoke this method // only the first callback is requied. The second // callback - the error handler - and the third // argument - our configuration options - are optional. navigator.geolocation.getCurrentPosition( function( position ){ // Check to see if there is already a location. // There is a bug in FireFox where this gets // invoked more than once with a cahced result. if (locationMarker){ return; } // Log that this is the initial position. console.log( "Initial Position Found" ); // Add a marker to the map using the position. locationMarker = addMarker( position.coords.latitude, position.coords.longitude, "Initial Position" ); }, function( error ){ console.log( "Something went wrong: ", error ); }, { timeout: (5 * 1000), maximumAge: (1000 * 60 * 15), enableHighAccuracy: true } ); // Now tha twe have asked for the position of the user, // let's watch the position to see if it updates. This // can happen if the user physically moves, of if more // accurate location information has been found (ex. // GPS vs. IP address). // // NOTE: This acts much like the native setInterval(), // invoking the given callback a number of times to // monitor the position. As such, it returns a "timer ID" // that can be used to later stop the monitoring. var positionTimer = navigator.geolocation.watchPosition( function( position ){ // Log that a newer, perhaps more accurate // position has been found. console.log( "Newer Position Found" ); // Set the new position of the existing marker. updateMarker( locationMarker, position.coords.latitude, position.coords.longitude, "Updated / Accurate Position" ); } ); function codeLatLng( latitude, longitude ) { if( ! geocoder ) { geocoder = new google.maps.Geocoder(); } var latlng = new google.maps.LatLng( latitude, longitude ); geocoder.geocode({'latLng': latlng}, function( results, status ) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { var postalCode; for( var acx = 0; acx < results[1].address_components.length; acx++ ) { var ac = results[1].address_components[acx]; if( ac.types[0] == "postal_code" ) { postalCode = ac.short_name; } } if( postalCode ) { Fse.Mobile.setCookie( "geoPostalCode", postalCode ); } else { // Fse.Mobile.setCookie( "geoPostalCode", "" ); } } else { alert("No results found"); } } else { alert("Geocoder failed due to: " + status); } }); } // If the position hasn't updated within 5 minutes, stop // monitoring the position for changes. setTimeout( function(){ // Clear the position watcher. navigator.geolocation.clearWatch( positionTimer ); }, (1000 * 60 * 5) ); } else { console.log( "No geolocation" ); } // I add a marker to the map using the given latitude // and longitude location. function addMarker( latitude, longitude, label ){ var marker = {}; marker.latitude = latitude; marker.longitude = longitude; marker.label = label; // Return the new marker reference. codeLatLng( marker.latitude, marker.longitude ); return( marker ); } // I update the marker's position and label. function updateMarker( marker, latitude, longitude, label ){ // Update the position. marker.latitude = latitude; marker.longitude = longitude; marker.label = label; }