Javascript 如何通过一次性权限请求查看传单js上的用户位置?

Javascript 如何通过一次性权限请求查看传单js上的用户位置?,javascript,geolocation,leaflet,user-permissions,markers,Javascript,Geolocation,Leaflet,User Permissions,Markers,我希望每次都用用户位置更新标记。 但是,我并不总是希望每5分钟寻求一次用户权限,但我希望在用户停止使用应用程序之前寻求一次用户权限。 有什么想法吗 下面是我每3分钟请求一次的代码 // placeholders for the L.marker and L.circle representing user's current position and accuracy var current_position, current_accuracy; function onLocation

我希望每次都用用户位置更新标记。 但是,我并不总是希望每5分钟寻求一次用户权限,但我希望在用户停止使用应用程序之前寻求一次用户权限。 有什么想法吗

下面是我每3分钟请求一次的代码

// placeholders for the L.marker and L.circle representing user's current position and accuracy    
var current_position, current_accuracy;

function onLocationFound(e) {
  // if position defined, then remove the existing position marker and accuracy circle from the map
  if (current_position) {
      mymap.removeLayer(current_position);
      mymap.removeLayer(current_accuracy);
  }

  var radius = e.accuracy / 10;

  current_position = L.marker(e.latlng).addTo(mymap);
    

  current_accuracy = L.circle(e.latlng, radius).addTo(mymap);
}

function onLocationError(e) {
  alert(e.message);
}

mymap.once('locationfound', onLocationFound);
mymap.on('locationerror', onLocationError);

// wrap map.locate in a function    
function locate() {
  mymap.locate({setView: true, maxZoom: 16});
}

// call locate every 3 minutes... forever
setInterval(locate, 180000);

如果您在上查看传单的文档/源代码,它将使用浏览器的底层
navigator.geolocation.getCurrentPosition
API获取用户的位置。谷歌一直在寻求许可。通常,我们只想在一些显式的用户操作上调用
getCurrentPosition
,比如单击事件。我无法准确复制您的问题,但我将时间间隔改为1秒。但是,我确实每秒都会在控制台中收到警告:

[违规]仅请求地理位置信息以响应用户手势。

传单允许您传递到
map.locate
功能,其中一个功能是
watch:true
,它将使用浏览器的
watchPosition
方法,该方法连续读取位置数据。您还可以将
maximumAge
timeout
传递给这些选项,这是一种续订间隔,但并不确切。为了更好地理解,我会看一看

它直接使用
watchPosition
api(与使用传单的
map.locate
wrapper相反)。检查控制台,您将看到用户位置的定期日志。页面加载时,只要求用户一次允许位置权限,但返回位置的频率不低于
超时值


希望这能让您走上正轨。

如果您在上查看传单的文档/源代码,它将使用浏览器的底层
navigator.geolocation.getCurrentPosition
API获取用户的位置。谷歌一直在寻求许可。通常,我们只想在一些显式的用户操作上调用
getCurrentPosition
,比如单击事件。我无法准确复制您的问题,但我将时间间隔改为1秒。但是,我确实每秒都会在控制台中收到警告:

[违规]仅请求地理位置信息以响应用户手势。

传单允许您传递到
map.locate
功能,其中一个功能是
watch:true
,它将使用浏览器的
watchPosition
方法,该方法连续读取位置数据。您还可以将
maximumAge
timeout
传递给这些选项,这是一种续订间隔,但并不确切。为了更好地理解,我会看一看

它直接使用
watchPosition
api(与使用传单的
map.locate
wrapper相反)。检查控制台,您将看到用户位置的定期日志。页面加载时,只要求用户一次允许位置权限,但返回位置的频率不低于
超时值


希望这能让你走上正轨。

你尝试过什么?你能给我们看一些代码吗?好的,让我用代码编辑这个问题你试过什么?你能给我们看一些代码吗?好的,让我用代码编辑这个问题