Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 0.81 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // geo-location shim
  2.  
  3. // currentely only serves lat/long
  4. // depends on jQuery
  5.  
  6. ;(function(geolocation){
  7.  
  8.   if (geolocation) return;
  9.  
  10.   var cache;
  11.  
  12.   geolocation = window.navigator.geolocation = {};
  13.   geolocation.getCurrentPosition = function(callback){
  14.    
  15.     if (cache) callback(cache);
  16.    
  17.     $.getScript('//www.google.com/jsapi',function(){
  18.      
  19.       cache = {
  20.         coords : {
  21.           "latitude": google.loader.ClientLocation.latitude,
  22.           "longitude": google.loader.ClientLocation.longitude
  23.         }
  24.       };
  25.      
  26.       callback(cache);
  27.     });
  28.    
  29.   };
  30.  
  31.   geolocation.watchPosition = geolocation.getCurrentPosition;
  32.  
  33. })(navigator.geolocation);
  34.  
  35.  
  36.  
  37. // usage
  38. navigator.geolocation.watchPosition(function(pos){
  39.   console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
  40. });