2015-06-23 5 views
3

Работа в настоящее время с HTML5 Geolocation, и я протестировал его на всех веб-браузерах и, похоже, работает. Однако, когда я тестирую Geolocation на iPad, он работает на iPad mini все время, но когда я кладу его на iPad (iPad 2), местоположение, похоже, не работает все время.HTML5 Geolocation - не работает все время на iOS

Я пытаюсь сделать эту веб-страницу, чтобы решение можно было портировать на несколько платформ, а не только на iOS.

Edit:

Только что попробовал, он работает в сафари браузер, но это просто не работает внутри приложения 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> 
+0

, что вы имеете в виду, что, кажется, не работает? где функция, в которую он попал? –

+0

Что такое версия ios? –

+0

@RemingHsu обновил мой вопрос, он сообщает вам, где должно отображаться местоположение, а функция находится внутри '$ (document) .ready', а iOS - 8,3 –

ответ

1

Я думал, вы должны получить разрешение на местоположение и в первой.

Добавить 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><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> 
+0

Я уже пробовал это, похоже, это не помогло iPads –

+0

Вы проверили «Настройки»> «Конфиденциальность»> «Службы местоположения»? –

+0

Да Я проверил настройки конфиденциальности, Всегда разрешаю местоположение приложения. Иногда он работает, но иногда он просто перестает работать, как будто нет службы. Это то, что я пытаюсь понять, почему это иногда работает, а иногда и не –