Skip to content

2gis/mock-geolocation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mock-geolocation

Mock for navigator.geolocation.

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.