How to prevent Android Mock Location in PWA application?

1.7k Views Asked by At

In PWA, we can get device's current position with W3C geolocation API. It is easy.

  if (!navigator.geolocation) {
    status.textContent = 'Geolocation is not supported by your browser';
  } else {
    status.textContent = 'Locating…';
    navigator.geolocation.getCurrentPosition(success, error);
  }

But, in Android, W3C location API is affected by user's mock location setting. So user easily able to spoof there current position.

Are there any way to prevent mock location setting in PWA?

2

There are 2 best solutions below

1
sparkle ramani On

check mock location using this condition:

 public static boolean isMockLocation(Location location) {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && location != null && location.isFromMockProvider();
    }
6
1qazxsw2 On

If you are afraid users will spoof their location, Mock location is not their only option. They can use a gps-sdr-sim and HackRF SDR to generate fake GPS signals and transmit them over the air.