HTML5 地理定位 - 在 iOS 上一直不工作

HTML5 Geolocation - Not working all the time on iOS

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

我正在尝试在 Web 端进行此操作,以便可以将解决方案移植到多个平台,而不仅仅是 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>

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

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>