【问题标题】:Iphone HTML5 Geolocation always return a errorIphone HTML5 Geolocation 总是返回错误
【发布时间】:2011-09-10 09:38:02
【问题描述】:

我尝试在 Iphone 上使用 html5 地理位置,但它总是返回错误。

var win = function(position) { alert('一切都很好!' + position) };
var fail = function(e) { alert('Can\'t retrieve position.\nError: ' + e) };
navigator.geolocation.getCurrentPosition(win, fail);

我在 IOS 模拟器和我的设备上进行了测试,但无法正常工作。 在 Safari 上,我只能使用无线互联网连接 (??) 获取我的位置。

有办法让这项工作发挥作用吗?

【问题讨论】:

    标签: iphone html safari geolocation mobile-safari


    【解决方案1】:

    确保设备上的定位服务已针对 Safari 开启。转到设置 > 隐私 > 定位服务 > Safari 并确保将其设置为 ON

    然后试试这段代码:

    function doGPS()
    {
        try{
            //position request
            navigator.geolocation.getCurrentPosition( function (position) { 
            alert(position.coords.latitude);
            alert(position.coords.longitude);
            });
        }
        catch(evt)
        {
            alert(evt.message);
        }
    
    }
    

    【讨论】:

    • 开启定位服务对我有用。我尝试在以下位置对其进行测试:html5demos.com/geo 问题: Safari 的定位服务是默认打开还是关闭?
    【解决方案2】:

    我使用过 appMobi 的 js,如果我在 iPhone 上的 Safari 中重新加载网站,它就可以正常工作。但是,主屏幕添加书签的应用程序并没有这样做(即使我在网络应用程序上添加了新版本的书签)。

    新的 js 正在加载到书签应用程序中(我每次更新代码时都添加了一条带有新提示的弹出消息,以确保 Web 应用程序正在加载新的 js),并且 iPhone 会提示我如果我想允许网站检查,但当我说是时没有任何反应。而在 Safari 中,它会显示纬度/经度。

    有人遇到过类似的问题吗?

    更新:它有效,但仅在您第一次加载页面时 - 第二次显示“开始”,但不会显示纬度/经度...

    if (navigator.geolocation) {
      alert('Geolocation is supported!');
    }
    else {
      alert('Geolocation is not supported for this Browser/OS version yet.');
    }
    
    try{
        //position request
       alert('start');
        navigator.geolocation.getCurrentPosition( function (position) { 
        alert(position.coords.latitude);
        alert(position.coords.longitude);
        });
    }
    catch(evt)
    {
        alert('fail'+evt.message);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多