Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chelovekula/ac590bc76d461313a771f2562b604918 to your computer and use it in GitHub Desktop.
Save chelovekula/ac590bc76d461313a771f2562b604918 to your computer and use it in GitHub Desktop.
Mocking/Spoofing Browser Geolocation W3C API Call GpsMock.mock() to mock/spoof
var GpsMock = {
mockSetIntervalId : null,
mock : function( x, y ) {
var setIntervalId;
if( navigator.geolocation ) {
var self = this;
if( !x ) {
//start x y not set, try to get 'REAL' current location and use it
navigator.geolocation.getCurrentPosition(
function success(position) { self.mock(position.coords.longitude, position.coords.latitude) },
function fail(position) { alert('error: geolocation might not be supported'); },
{}
);
return;
}
var position = {
coords : {
latitude: y,
longitude: x,
accuracy: 10
},
timestamp: (new Date()).valueOf()
}
var LOC_UPDATE_INTERVAL = 500 /*ms*/;
navigator.geolocation.watchPosition = function( successFn, errorFn, options ) {
var broadcast = function( offsetX, offsetY, offsetTimestamp ) {
if( ! offsetX ) { offsetX = 0.0001; }
if( ! offsetY ) { offsetY = 0.0001 ; }
if( ! offsetTimestamp ) { offsetTimestamp = LOC_UPDATE_INTERVAL; }
position.coords.longitude += offsetX;
position.coords.latitude += offsetY;
position.timestamp += offsetTimestamp;
successFn(position);
}
broadcast();
self.mockSetIntervalId = window.setInterval( broadcast, LOC_UPDATE_INTERVAL );
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment