HTML5地理位置 - 无法在iOS上一直运行 [英] HTML5 Geolocation - Not working all the time on iOS

查看:149
本文介绍了HTML5地理位置 - 无法在iOS上一直运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用HTML5地理位置,并且我已在所有网络浏览器上测试过它,并且它似乎正在工作。但是,当我在iPad上测试地理位置时,它始终适用于iPad mini,但是当我将它放在较大的iPad(iPad 2)上时,该位置似乎始终无法正常工作。

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

编辑:

只是没有在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>
//示例
$(document).ready(function(){
setInterval(function(){
getLocation(function(position){
// do
currentLat = position.coords.latitude;
currentLng = position.coords.longitude;

$(#status)。html(currentLat + + currentLng);
});
},1000);
});


var GPSTimeout = 10; // init global var注意:我注意到10给了我最快的结果,但是用这个数字来玩你自己喜欢的


//函数来调用你想要的位置回调(位置)
函数getLocation(回调)
{
if(navigator.geolocation)
{
var clickedTime =(new Date())。getTime ); //获取当前时间
GPSTimeout = 10; //重置超时以防万一你多次调用它
ensurePosition(callback,clickedTime); //调用递归函数来获取位置

}
return true;


//递归位置函数
函数ensurePosition(回调,时间戳)
{
if(GPSTimeout< 6000)//设置为点你想放弃
{
//调用地理定位函数
navigator.geolocation.getCurrentPosition(
function(position)// on success
{
//如果返回的时间戳减去被调用时设置的时间大于0,那么位置是最新的
if(position.timestamp - timestamp> = 0)
{
GPSTimeout = 10; //重置超时以防万一
回调(位置); //调用您创建的回调函数
}
else //返回的gps是不是当前的,需要刷新
{
GPSTimeout + = GPSTimeout; //增加超时本身n * 2
确保位置(回调,时间戳); //调用自己刷新
}
},
function()//错误:gps失败,所以我们会再试一次
{
GPSTimeout + = GPSTimeout; //增加超时本身n * 2
ensurePosition(callback,timestamp); //调用自己再试一次
},
{maximumAge:0,timeout:GPSTimeout}

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

<! - 页面标题 - >
< h1> Geolocation< / h1>

<! - 状态 - >
< p>寻找您的位置:< span id =status>检查...< / span>< / p>

< / body>
< / html>


解决方案

我以为你应该先获得地点的许可。



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



试试上面的内容,看看是否有帮助。



简单无计时器

 <!DOCTYPE html> 
< html>
< head>
< script src =http://code.jquery.com/jquery-1.11.3.min.js>< / script>
< / head>
< body>
< BR>< BR>< BR>
< button onclick =getLocation()> get Location< / button>
< script>
函数getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(alertPosition);
} else {
alert(不支持地理定位。);



function alertPosition(position){
alert(Latitude:+ position.coords.latitude +
< br>经度:+ position.coords.longitude);
}
< / script>
< / body>
< / html>


Working currently with the HTML5 Geolocation and I've tested it on all web browsers and it seems to be working. However when I test the Geolocation on the iPad, it works for the iPad mini all the time, but when I put it on the bigger iPad (iPad 2) the location doesn't seem to work all the time.

I'm trying to do this web-side so that the solution can be ported over to multiple platforms and not just iOS.

Edit:

Just tried, It works in the safari browser but it's just not working inside the iOS application.

Does anyone have any ideas why it's not working?

Internet: Tried Wi-Fi and tried hotspot, and also tried Wi-Fi turned on without connecting to anyones.

iOS version: 8.3

The location should be displayed here:

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

Code:

<!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>

解决方案

I thought you should get location's permission first.

Add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in App-Info.plist and give it a string.

Try with above, and see if that helps.

simple for no timer

<!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>

这篇关于HTML5地理位置 - 无法在iOS上一直运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆