Created
May 30, 2013 02:28
-
-
Save lydonchandra/5675402 to your computer and use it in GitHub Desktop.
Mocking/Spoofing Browser Geolocation W3C API Call GpsMock.mock() to mock/spoof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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