如何通过 link 一次单击连续获取位置

How to get a location continuously by link click at once

我正在尝试通过 URL 访问获取移动用户位置。使用 PHP $_SERVER 变量我可以获得 ipv4/ipv6 地址,但结果不太准确。

with the one click of user javascript can send a client location. but it's asks for the user permission, it's fine. but after the one click, expecting to get a location continuously, even the browser is not active

你必须使用 web worker.

网络工作者是在后台运行的JavaScript,独立于其他脚本,不会影响页面的性能。当 Web Worker 在后台运行时,您可以继续做任何您想做的事情:点击、选择内容等。

现在 PWAs 使用 service workers(一种网络工作者)即使在应用程序关闭时也能收到通知。

您可以在起始页的 PWA 中调用以下代码。这样每次用户按照您提供的 link 打开它时,该应用程序将获得 her/his 位置:

function getLocation() {

  // Check if the current device support geoLocation
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(pos) {
      console.log("Latitude: " + pos.coords.latitude +
        "Longitude: " + pos.coords.longitude);
    });
  } else {
  // Geolocation is not supported by the
  }
}

这里是MSDN Docs about Gelocation.