地理位置权限弹出窗口-jquery

发布于 2025-02-08 07:44:51 字数 1723 浏览 1 评论 0原文

要求用户权限访问当前位置要阻止或允许的弹出窗口仅显示2秒钟,甚至在用户选择之前就关闭了。我需要帮助显示至少用户的弹出窗口

弹出式映像

    var listenType = (navigator.userAgent.toLowerCase().indexOf('edge') == -1) ? 'mouseup' : 'click';
...
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      getZipFromLatLng(position.coords.latitude, position.coords.longitude);
      
    }, (err) => {
      if (err.code === err.PERMISSION_DENIED) {
        const locationDisabledcontent = `          
        <div class="text-center zip-alert-heading"><b>Location sharing is disabled on your
        browser.</b></div>
      <p class="text-center zip-alert-subheading">Please enable location sharing in your browser settings. <br /> or
      type in a zip code
      to continue.</p>`;
        if ($('.custom-alert')) {
          $('.custom-alert .modal-body').empty();
          $('.custom-alert .modal-body').html(locationDisabledcontent);
          $('.custom-alert').addClass('show');
        }
        $('.custom-alert-close').on('click', function () {
          $('.custom-alert').removeClass('show');
        })
      }
    }, {timeout:5000});
  }
}
...

$('.zipEntry-text').on(listenType, function (e) {
  console.log('in zipentrytext, get current location');
  e.stopPropagation();
  e.preventDefault();
  getLocation();
})

$(document).ready(function() {
  const zipExists = getCookie('zip');
  if (!zipExists) {
    getLocation();
    const zipExists = getCookie('zip');
    if (!zipExists) {
      getAddressFromZip(defaultZip);
    }
  }
})

The popup that asks for user permission to access current location either to block or allow is showing for only 2 seconds it closes even before user could select. I need help in displaying the popup for atleast user to click on it

popup image

    var listenType = (navigator.userAgent.toLowerCase().indexOf('edge') == -1) ? 'mouseup' : 'click';
...
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      getZipFromLatLng(position.coords.latitude, position.coords.longitude);
      
    }, (err) => {
      if (err.code === err.PERMISSION_DENIED) {
        const locationDisabledcontent = `          
        <div class="text-center zip-alert-heading"><b>Location sharing is disabled on your
        browser.</b></div>
      <p class="text-center zip-alert-subheading">Please enable location sharing in your browser settings. <br /> or
      type in a zip code
      to continue.</p>`;
        if ($('.custom-alert')) {
          $('.custom-alert .modal-body').empty();
          $('.custom-alert .modal-body').html(locationDisabledcontent);
          $('.custom-alert').addClass('show');
        }
        $('.custom-alert-close').on('click', function () {
          $('.custom-alert').removeClass('show');
        })
      }
    }, {timeout:5000});
  }
}
...

$('.zipEntry-text').on(listenType, function (e) {
  console.log('in zipentrytext, get current location');
  e.stopPropagation();
  e.preventDefault();
  getLocation();
})

$(document).ready(function() {
  const zipExists = getCookie('zip');
  if (!zipExists) {
    getLocation();
    const zipExists = getCookie('zip');
    if (!zipExists) {
      getAddressFromZip(defaultZip);
    }
  }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2025-02-15 07:44:51

默认值(getAddressfromzip)甚至在getCurrentLocation返回值之前运行

Default(getAddressfromzip) is running even before getcurrentlocation returns a value so I set a delay of 10seconds after getLocation so default could not run

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文