Roman Kiriluck
Roman Kiriluck

Reputation: 79

Geolocation for mobile devices

I have a problem with geolocation on idevices on my website.

Actually I just need to get latitude/longitude coordinates. On PC, android devices everything is cool, on iphone it also fine but only if I use wifi connection. But when i'm on 3g or LTE with my old iPhone 5s I simply get nothing (but starting from iphone 6 it works).

I've read that Safari is not supporting geolocation if wifi is turned off. But still what I need is to make it work on iDevices such as iPhone 4,5. I'm using this piece of example code:

<script>
    var x = document.getElementById("demo");
        function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position) {
        x.innerHTML = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude; 
    }
</script>

Upvotes: 0

Views: 3821

Answers (1)

mxmlc
mxmlc

Reputation: 479

I'm an iPhone 5S owner and tried the geolocation over 3G and it works like a charm. I've tried this CodePen over Wifi and over 3G without any issues.

$(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) {
    //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
      }
    )
  }
}

Thanks to Chris Beckett for the sample.

Maybe you can provide more details if you can't get it working yet?

Ps: just editing to make sure everyone who needs geolocation check if location services for Safari is enabled:

  • First check if Location Services is enabled under: Settings > Privacy > Location Services
  • Next check if it is enabled for Safari Websites

Upvotes: 2

Related Questions