Mock for navigator.geolocation
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
.gitignore rename project Mar 13, 2015
LICENSE.md Add MIT license Apr 20, 2016
README.md
README_RU.md update readme Mar 16, 2015
gulpfile.js rename project Mar 13, 2015
index.html add use/restore to demo Oct 31, 2014
index.js fix bug with browserify require Oct 31, 2014
package.json Release 1.0.11 Sep 1, 2015

README.md

Mock-geolocation

Mock for navigator.geolocation.

Russian readme

Demo

var point = [54.980206086231, 82.898068362003];

geolocate.use();

navigator.geolocation.getCurrentPosition(function(position) {
  assert(position.coords.latitude).equal(point[0]);
  assert(position.coords.longitude).equal(point[1]);
});

geolocate.send({lat: point[0], lng: point[1]});

geolocate.restore();

Installation

Manually:

<script src="geolocate.js"></script>

From npm:

npm install mock-geolocation

As CommonJS or AMD module:

var geolocate = require('mock-geolocation');

API

.use()

Replace the native navigator.geolocation object

.restore()

Restore navigator.geolocation in original state

.send([options])

This method emulates the finding position after calling getCurrentPosition and watchPosition method. Updates position from options which may include the following parameters from positions.coords and timestamp.

navigator.geolocation.getCurrentPosition(function(position) {
  console.log(position);
});

geolocate.send({
  latitude: 50,
  longitude: 10,
  accuracy: 5,
  timestamp: 3000
});

/* {
  coords: {
    accuracy: 5,
    altitude: null
    altitudeAccuracy: null,
    heading: null,
    latitude: 50
    longitude: 10,
    speed: null
  },
  timestamp: 3000
} */

navigator.geolocation.getCurrentPosition(function(position) {
  console.log(position);
});

geolocate.send();

/* Show same position {
  coords: {
    accuracy: 5,
    altitude: null
    altitudeAccuracy: null,
    heading: null,
    latitude: 50
    longitude: 10,
    speed: null
  },
  timestamp: 3000
} */

.change(options)

Change current position and call success callback of watchPosition method. Updates position from options which may include the following parameters from positions.coords and timestamp.

navigator.geolocation.watchPosition(function(position) {
  console.log(position.coords.latitude + ', ' + position.coords.longitude);
});

geolocate.send();
// 54.9799, 82.89683699999999

geolocate.change({lat: 10, lng: 15});
// 10, 15

geolocate.change({lat: 25});
// 25, 15

.sendError([options])

Call error callback of getCurrentPosition method. options may include the parameters code and message.

.changeError([options])

Call error callback of watchPosition method. options may include the parameters code and message.