HTML5 地理位置 - 在 iOS 上无法一直工作

Posted

技术标签:

【中文标题】HTML5 地理位置 - 在 iOS 上无法一直工作【英文标题】:HTML5 Geolocation - Not working all the time on iOS 【发布时间】:2015-06-23 08:58:08 【问题描述】:

目前正在使用 html5 地理位置,我已经在所有网络浏览器上对其进行了测试,它似乎可以正常工作。但是,当我在 iPad 上测试 Geolocation 时,它一直适用于 iPad mini,但是当我将它放在更大的 iPad (iPad 2) 上时,该位置似乎并不总是有效。

我正在尝试做这个网络端,以便可以将解决方案移植到多个平台,而不仅仅是 ios

编辑:

刚刚试过,在 safari 浏览器中可以使用,但在 iOS 应用程序中无法使用。

有没有人知道为什么它不起作用?

互联网:尝试了 Wi-Fi 和热点,还尝试了打开 Wi-Fi 而没有连接到任何人。

iOS 版本: 8.3

位置应该显示在这里:

$("#status").html(currentLat + " " + currentLng);

代码:

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.11.3.min.js"></script>
<script>
//example
$(document).ready(function()
    setInterval(function() 
        getLocation(function(position) 
            //do something cool with position
            currentLat = position.coords.latitude;
            currentLng = position.coords.longitude;

            $("#status").html(currentLat + " " + currentLng);
        );
    , 1000);
);


var GPSTimeout = 10; //init global var NOTE: I noticed that 10 gives me the quickest result but play around with this number to your own liking


//function to be called where you want the location with the callback(position)
function getLocation(callback)
   
    if(navigator.geolocation)
    
        var clickedTime = (new Date()).getTime(); //get the current time
        GPSTimeout = 10; //reset the timeout just in case you call it more then once
        ensurePosition(callback, clickedTime); //call recursive function to get position

    
    return true;


//recursive position function
function ensurePosition(callback, timestamp)

    if(GPSTimeout < 6000)//set at what point you want to just give up
    
        //call the geolocation function
        navigator.geolocation.getCurrentPosition(
            function(position) //on success
        
                //if the timestamp that is returned minus the time that was set when called is greater then 0 the position is up to date
            if(position.timestamp - timestamp >= 0)
                
                    GPSTimeout = 10; //reset timeout just in case
                    callback(position); //call the callback function you created
                
                else //the gps that was returned is not current and needs to be refreshed
                
                    GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
                    ensurePosition(callback, timestamp); //call itself to refresh
                
            ,
            function() //error: gps failed so we will try again
            
                GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
                ensurePosition(callback, timestamp);//call itself to try again
            ,
            maximumAge:0, timeout:GPSTimeout
        )
           

</script>
</head>
<body>
<div id="wrapper">

  <!-- Page heading -->
  <h1>Geolocation</h1>

  <!-- Status -->
  <p>Finding your location: <span id="status">checking...</span></p>

  </body>
</html>

【问题讨论】:

你的意思是它似乎不起作用?它的功能在哪里? 什么是ios版本? @RemingHsu 更新了我的问题,它告诉你应该在哪里显示位置,函数在$(document).ready 内部,iOS 是 8.3 【参考方案1】:

我认为你应该先获得位置许可。

App-Info.plist 中添加NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription 并给它一个字符串。

尝试以上方法,看看是否有帮助。

简单的没有计时器

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>  
</head>
<body>
    <BR><BR><BR><BR>
    <button onclick="getLocation()">get Location</button>
    <script>
        function getLocation() 
            if (navigator.geolocation) 
                navigator.geolocation.getCurrentPosition(alertPosition);
             else 
                alert("Geolocation is not supported.");
            
        

    function alertPosition(position) 
        alert("Latitude: " + position.coords.latitude +
              "<br>Longitude: " + position.coords.longitude);
    
    </script>
</body>
</html>

【讨论】:

我已经尝试过了,它似乎对更大的 iPad 没有帮助 您是否检查了“设置”>“隐私”>“定位服务”? 是的,我检查了隐私设置,始终允许应用程序的位置。它有时可以工作,但有时它就像没有服务一样停止工作。这就是我要解决的问题,为什么它有时有效,有时无效 见:***.com/questions/3397585/… 你能告诉我你的 iOS 端吗,因为你将使用 CLLocationManager 对吗?这是看看你有没有和我不一样的东西

以上是关于HTML5 地理位置 - 在 iOS 上无法一直工作的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Android 4.4 中使用 HTML5 地理定位

为啥这个 HTML5 地理定位代码不能仅在 iOS 上获取用户的位置?

在 iOS 上使用带有 Phonegap 的 HTML5 缓存清单

HTML5 地理定位在 Safari 上不起作用 - Mac OS/iOS

HTML5 视频无法在 IOS 设备上播放,但在其他任何地方都可以正常播放

嵌入的 YouTube 视频无法在 iPad (iOS 7) 上播放,而 HTML5 搜索输入可见