Jan19

Mock geolocation

Useful if you’re working on a website meant for mobile devices. Firefox 3.5 has geolocation, but I use this with Safari and GreaseKit. Replace with whatever latitude and longitude you prefer, naturally.

if (!('geolocation' in navigator)) {
  navigator.geolocation = {
    watchPosition: function(success, f, options) {
      var broadcast = function() {
        var position = {
          coords: {
            latitude: 30.2696384,
            longitude: -97.74947,
            accuracy: 10000,
          },
          timestamp: (new Date()).valueOf()
        };
        success(position);
      };

      broadcast();
      window.setInterval(broadcast, 10000);
    }
  };
}

Comments

  1. Nice! This is going to be really handy since I’m stuck on Firefox 3.0 at work, and doing lots of map-related tasks - geolocation is becoming increasingly useful and I think it’s definitely going to take off this year, for desktops as well as mobile devices.

    Shouldn’t you also ‘mirror’ the getCurrentPosition() method?

  2. Yes, I should, but I never used that method, so I didn’t bother to do so. ;-)

Painfully Obvious was built with WordPress, Prototype, Slicehost, and other accoutrements. Colophon →