How does Google Chrome know my current GPS location and how to use it in my code?

Today I understand that some pages can find me (to them using a laptop without built-in GPS) using google chrome with very impressive accuracy, I know that using your IP address, you can find me, but not with that purpose. Now in google maps you will see a button above the yellow men (street view) that will find you using google chrome .

How it works? Is there an API to use?

+3
source share
3 answers

HTML 5 Geolocation ( ):

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  alert("Not Supported!");
}

function success(position) {
  console.log(position.coords.latitude);
  console.log(position.coords.longitude);
}

function error(msg) {
  console.log(typeof msg == 'string' ? msg : "error");
}

var watchId = navigator.geolocation.watchPosition(function(position) {  
  console.log(position.coords.latitude);
  console.log(position.coords.longitude);
});

navigator.geolocation.clearWatch(watchId);

, Sascha

+4

, Google , WiFi, ; ; IP- . Google .

+3

Source: https://habr.com/ru/post/1756738/


All Articles