9

当用户访问我的网站时,我会检查是否在 PHP 中设置了位置会话。如果没有设置用户位置的会话,我将他们重定向到 www.domain.net/location。在此页面上有许多选项供用户选择位置。如果浏览器允许,其中一个选项是使用浏览器进行设置。下面的代码工作得很好。

我遇到的问题是,一旦用户允许浏览器共享位置,这似乎是永远设置的。当用户在浏览时,他们可能决定改变他们的位置,然后他们点击一个按钮再次转到 www.domain.net/location 页面。在没有任何弹出的情况下,它会自动获取浏览器位置并重定向它们。页面上还有其他位置选项,包括可点击的地图,我真的需要能够重置、过期、删除、强制执行某些操作,以便浏览器再次询问用户是否想使用浏览器设置位置或允许用户使用其他选项之一。

我唯一能想到的是将下面的实际 Javascript 移动到另一个页面上,并且在 www.domain.net/location 页面上有另一个按钮,上面写着“使用我的精确位置”,然后链接到下面的 JS .

有谁知道重置这个或其他东西的另一种方法?

<script type="text/javascript" charset="utf-8">


    function findLocation()
    {
      var options = {timeout: 60000};
      navigator.geolocation.getCurrentPosition(foundLocation, noLocation, options);
    }

    function foundLocation(position)
    {
      var lat = position.coords.latitude;
      var lng = position.coords.longitude;

      //alert('Found location: ' + lat + ', ' + lng);
      window.location = "http://www.domain.net/?lat=" + lat + "&lng=" + lng;
    }

    function noLocation()
    {
      alert('Could not find location');
      window.location = "http://www.domain.net/location";
    }

    findLocation();


  </script>
4

4 回答 4

2

This is a per-browser setting, not something that you can control on the script side. What you may be able to do is provide a link such as this one that explains how to update geolocation settings for particular websites for the given browser.

It seems like you can get around your specific problem not by linking to /location, but linking to /location?update or something like that. Essentially, go to the location page but add something that prevents the automatic redirect so the user can make a different decision about their location.

于 2013-02-19T11:47:01.377 回答
1

诚然,我只玩过几次 Javascript Location API,但我的理解是浏览器存储用户偏好(这个假设似乎在这里得到了加强)。

引用的页面表明用户的偏好是基于每个站点(域)而不是每个 URL 存储的。如果不是这种情况,那么向 URL 添加一个随机字符串值应该可以解决问题。编辑:我已经检查并且每个域都定义了豁免:

That would make your suggestion of deferring the code to another page - or binding it to an event that doesn't run on page load the best solution I can think of off-hand.

于 2013-02-19T11:42:26.860 回答
1

If the user gives your site permission to use its location, it simply stores "allow" along with your site url in the browser security settings. It doesn't store the actual location. That is done via code. If you want to use a new location, just get it with javascript.

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

See here for more on getting geolocation: https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation

If you're storing the location in a cookie and re-using that location, just clear your cookie and reset it, or don't use cookies at all.

To see Chrome's storage of geo location settings, open your Chrome options "triple bar" > Settings > advanced... > under privacy, click Content Settings > under Location, click Manage Exceptions.

于 2014-04-18T16:11:50.953 回答
0

I have decided to add ?change-location to the end of the URL and use PHP to check for this and then hide the Javascript on the page. This is not a great fix but it does look like this is a per browser setting and not something that you can reset using script. It would seem that once the user has said 'remember my preference' then thats what it will do until they dig into setting and change this on their browser.

于 2013-02-19T12:26:08.733 回答