Skip to content

Instantly share code, notes, and snippets.

@tomasdev
Forked from paulirish/gist:366184
Created July 23, 2012 13:19

Revisions

  1. @paulirish paulirish revised this gist Jul 13, 2012. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    // currentely only serves lat/long
    // depends on jQuery

    // doublecheck the ClientLocation results because it may returning null results

    ;(function(geolocation){

    if (geolocation) return;
    @@ -16,12 +18,15 @@

    $.getScript('//www.google.com/jsapi',function(){

    // sometimes ClientLocation comes back null
    if (google.loader.ClientLocation) {
    cache = {
    coords : {
    "latitude": google.loader.ClientLocation.latitude,
    "longitude": google.loader.ClientLocation.longitude
    }
    };
    }

    callback(cache);
    });
    @@ -37,4 +42,4 @@
    // usage
    navigator.geolocation.watchPosition(function(pos){
    console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
    });
    });
  2. @paulirish paulirish revised this gist Sep 16, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@

    var cache;

    geolocation = {};
    geolocation = window.navigator.geolocation = {};
    geolocation.getCurrentPosition = function(callback){

    if (cache) callback(cache);
  3. @paulirish paulirish revised this gist Sep 16, 2010. 1 changed file with 33 additions and 26 deletions.
    59 changes: 33 additions & 26 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,40 @@
    // geo-location bridge
    function getLocation(callback){
    // geo-location shim

    // currentely only serves lat/long
    // depends on jQuery

    ;(function(geolocation){

    if (geolocation) return;

    if (getLocation.cache) return callback(getLocation.cache);

    if (Modernizr.geolocation) {
    var geocallback = function(position) {
    callback(getLocation.cache = {
    "lat": position.coords.latitude,
    "lon": position.coords.longitude,
    "obj": position
    })
    };
    var cache;

    geolocation = {};
    geolocation.getCurrentPosition = function(callback){

    navigator.geolocation.getCurrentPosition(geocallback);
    navigator.geolocation.watchCurrentPosition(geocallback);
    if (cache) callback(cache);

    } else {

    $.getScript('//www.google.com/jsapi',function(){
    callback(getLocation.cache = {
    "lat": google.loader.ClientLocation.latitude,
    "lon": google.loader.ClientLocation.longitude,
    "obj": google.loader.ClientLocation
    })

    cache = {
    coords : {
    "latitude": google.loader.ClientLocation.latitude,
    "longitude": google.loader.ClientLocation.longitude
    }
    };

    callback(cache);
    });
    }
    }

    };

    geolocation.watchPosition = geolocation.getCurrentPosition;

    })(navigator.geolocation);



    // usage
    getLocation(function(pos){
    console.log("I'm located at ",pos.lat,' and ',pos.lon);
    })
    navigator.geolocation.watchPosition(function(pos){
    console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
    });
  4. @paulirish paulirish revised this gist May 16, 2010. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,16 @@ function getLocation(callback){
    if (getLocation.cache) return callback(getLocation.cache);

    if (Modernizr.geolocation) {

    navigator.geolocation.getCurrentPosition(function(position) {
    var geocallback = function(position) {
    callback(getLocation.cache = {
    "lat": position.coords.latitude,
    "lon": position.coords.longitude,
    "obj": position
    })
    });
    };

    navigator.geolocation.getCurrentPosition(geocallback);
    navigator.geolocation.watchCurrentPosition(geocallback);

    } else {

  5. @paulirish paulirish revised this gist Apr 14, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ function getLocation(callback){

    } else {

    $.getScript('http://www.google.com/jsapi',function(){
    $.getScript('//www.google.com/jsapi',function(){
    callback(getLocation.cache = {
    "lat": google.loader.ClientLocation.latitude,
    "lon": google.loader.ClientLocation.longitude,
  6. @paulirish paulirish created this gist Apr 14, 2010.
    31 changes: 31 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    // geo-location bridge
    function getLocation(callback){

    if (getLocation.cache) return callback(getLocation.cache);

    if (Modernizr.geolocation) {

    navigator.geolocation.getCurrentPosition(function(position) {
    callback(getLocation.cache = {
    "lat": position.coords.latitude,
    "lon": position.coords.longitude,
    "obj": position
    })
    });

    } else {

    $.getScript('http://www.google.com/jsapi',function(){
    callback(getLocation.cache = {
    "lat": google.loader.ClientLocation.latitude,
    "lon": google.loader.ClientLocation.longitude,
    "obj": google.loader.ClientLocation
    })
    });
    }
    }

    // usage
    getLocation(function(pos){
    console.log("I'm located at ",pos.lat,' and ',pos.lon);
    })