【问题标题】:geolocation not working when location service toggled off and on位置服务关闭和打开时地理定位不起作用
【发布时间】:2016-10-11 21:47:56
【问题描述】:

我有一个由按钮单击触发的功能,用于检查地理位置。当地理位置处于打开状态时,它可以在手机上正常工作,而在关闭时,会按预期显示一条消息。当首先关闭手机的位置服务,单击按钮时出现问题(消息弹出,如预期的那样),然后如果用户在应用程序仍处于打开状态时重新打开位置服务,再次单击按钮,仍然相同弹出“无定位服务”消息。

有没有办法在每次点击按钮时检查手机的定位服务是打开还是关闭?在 Android 和 IOS 上获得相同的结果。

代码:

$(document).ready(function () {

    $('#smallScreenGeolocate').on('click', function(){

     getCurrentLocation();
     });
});

function getCurrentLocation () {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(addGeolocationMarker, locationError);
    return true;
}
else {
    alert("Browser doesn't support Geolocation. Visit http://caniuse.com to discover browser support for the Geolocation API.");
    return false;
}
}

【问题讨论】:

  • 没有人吗?我找不到任何东西来解决这个问题......似乎很奇怪,当 web 应用程序打开时我无法检测到地理定位是否已打开或关闭......?
  • 我们在谈论某种框架吗?像 phonegap、cordova、xamarin 还是一个简单的 Web 应用程序?
  • 如果您刷新页面(位置处于活动状态),那么可以访问位置详细信息吗?
  • @ddb 是的,.. 我实际上并没有为 webapp 开发包装器,但我相信他们正在使用 xamarin ......但我有 webapp 本身的问题;我需要刷新页面才能检测到地理位置。

标签: android ios events geolocation location-services


【解决方案1】:

从另一个 SO 帖子 https://stackoverflow.com/a/14862073/6539349 中检查这个答案

你必须检查这里建议的错误是什么http://www.w3schools.com/html/html5_geolocation.asp

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
   x.innerHTML = "Latitude: " + position.coords.latitude +
   "<br>Longitude: " + position.coords.longitude;
}

getCurrentPosition() 方法的第二个参数showError 用于处理错误。它指定了在获取用户位置失败时要运行的函数:

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}

【讨论】:

  • 打开本地化后点击按钮时,应用程序是否显示其中一个错误?如果是,是哪一个?
  • @ddb 它在刷新时可以工作,但这还不够,因为 webapp 将使用应用程序包装器在 Apple Store 上用作应用程序......我已经得到了上面的所有错误代码;如果 webapp 已打开,则在打开地理定位后我会收到相同的错误。我的代码与上面在应用程序中给出的代码非常相似。这并不能解决我的问题。
  • 另外给出的 SO 答案也不能解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
相关资源
最近更新 更多