3

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>
YaBCK
  • 2,949
  • 4
  • 32
  • 61

1 Answers1

2

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>
Reming Hsu
  • 2,215
  • 15
  • 18
  • I have already tried this, it didn't seem to help on the bigger iPads – YaBCK Jun 24 '15 at 07:35
  • 1
    Did you check Settings > Privacy > Location Services? – Reming Hsu Jun 24 '15 at 07:51
  • Yes I've checked the privacy settings, Always allow for the location for the application. It sometimes works, but then sometimes it just stops working like there is no service. That's what i'm trying to work out, why it sometimes works and sometimes doesn't – YaBCK Jun 24 '15 at 07:58
  • See:http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt – Reming Hsu Jun 24 '15 at 08:04
  • Could you show me your iOS Side because you'll be using CLLocationManager right? This is see if you've got anything different than me – YaBCK Jun 24 '15 at 08:37
  • i didn't use CLLocationManager. i just use UIWebView to show the html,and i modify `src="http://code.jquery.com/jquery-1.11.3.min.js"`,and try with answer. – Reming Hsu Jun 24 '15 at 08:39
  • Your solution works on iPad mini like expected like I did, however it doesn't always work on the iPad 2. It seems to loose the GPS every now and again. I think this is just a known bug – YaBCK Jun 24 '15 at 08:42
  • could you try the code? of couse with NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in .plist – Reming Hsu Jun 24 '15 at 09:02
  • I've just gone with Native solution, because the HTML5 Geolocation doesn't seem to work all the time on the Webview – YaBCK Jun 24 '15 at 09:47