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

我与影子孤独终老i 提交于 2019-12-05 21:41:44

It's also possible with the actual HTML 5 Geolocation functions (only if your browser supports):

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);

Greetings, Sascha

Check out the help page

The local network information used by Google Location Services to estimate your location includes information about visible WiFi access points, including their signal strength; information about your local router; your computer's IP address. The accuracy and coverage of Google Location Services will vary by location.

Here is a link to the Firefox code that allows you to access the Google Location Service: http://mxr.mozilla.org/mozilla1.9.1/source/dom/src/geolocation/NetworkGeolocationProvider.js

Enjoy!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!