1

How can I fake my geolocation coordinates in Chrome?

I know that this can be done manually using Chrome developer tools as explained here: https://developers.google.com/web/tools/chrome-devtools/device-mode/device-input-and-sensors

However, I need a code solution, preferably JavaScript or Python.

When the code is run, Google Maps and other geolocation sites should display fake location.

Ned Hulton
  • 477
  • 3
  • 12
  • 27
  • Possible duplicate of [How can I set my latitude and longitude for debugging the Geolocation API with Google Chrome?](https://stackoverflow.com/questions/5505617/how-can-i-set-my-latitude-and-longitude-for-debugging-the-geolocation-api-with-g) – Martin Zeitler Sep 21 '18 at 03:14

1 Answers1

-1

You should be able to override navigator.geolocation.getCurrentPosition().

The following snippet generates a link that sends you to a map of Tokyo:

navigator.geolocation.getCurrentPosition = (s, e) => {
  s({
    coords: {
      latitude: 35.6895,
      longitude: 139.6917
    }
  });
};

navigator.geolocation.getCurrentPosition((p) => {
  const {latitude, longitude} = p.coords;
  const a = `<a href="https://maps.google.com/maps?q=${latitude},${longitude}">Map</a>`;

  document.getElementById('map').innerHTML = a;
  console.log(a);
});
<div id="map"></div>
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156