Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation HTML5 enableHighAccuracy True , False or Best Option?

i have a problem about HTML5 geolocation feature. I use the code below to get location data. I use "enableHighAccuracy: false" option to work with Cell Based GPS feature. Accurancy is low but response it too fast. But some people always use Built-in GPS with their mobile phone, so this code does not work for them. Bu if i change accurency option as "enableHighAccuracy: true" it works for them. But this time, the code uses only built-in GPS. not CELL based GPS.

The question -> How can i do that : First, try to get position from Built-in GPS with timeout (e.g. 5000ms ) if position cannot be got in this time just look for Cell Based position for timeout (e.g. 10000ms) if position cannot be get in this time, return an error message .

Here is the code that i use now.

Thanks in advance.

    function getLocationfromGoogle() {
    navigator.geolocation.getCurrentPosition(
      function(pos) {
        $("#lat_field").val(pos.coords.latitude);
        $("#long_field").val(pos.coords.longitude);
        var geocoder = new google.maps.Geocoder();
        var latLng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
              geocoder.geocode({ 'latLng': latLng}, function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                //console.log(results[0].formatted_address);
                $("#adresim").val(results[0].formatted_address);
                }
                else {
            alert('Google convertion is not succesfully done.');  

            }
            });
      },function error(msg){

                alert('Please enable your GPS position future.');  

      },{maximumAge:600000, timeout:5000, enableHighAccuracy: false}

    ); 
    }
like image 849
Erhan H. Avatar asked Jan 29 '12 12:01

Erhan H.


2 Answers

The code mentioned here: http://jsfiddle.net/CvSW4/ did not work for me during error handling.

The reason is that the error functions accept a parameter named 'position' but use an object in the functions called 'error'.

function errorCallback_highAccuracy(position) { ... }
function errorCallback_lowAccuracy(position) { ... }

The solution to fix this was to switch the error methods to accept the input value as a parameter named 'error' and not 'position', since the error callbacks do not accept a position and throw an error object instead.

function errorCallback_highAccuracy(error) { ... }
function errorCallback_lowAccuracy(error) { ... }

I mention it here, because I could not post on the resulting example page and also, this is the location where I linked through to find the code sample mentioned above.

like image 197
peawormsworth Avatar answered Oct 22 '22 22:10

peawormsworth


You should also be aware that the implementation of this varies from phone OS to phone OS - what works on Android may or may not work on iOS, BlackBerry, WindowsPhone, etc.

You're almost there, you just need to:

  1. Specify enableHighAccuracy: true (you have it set to false)
  2. Handle the timeout error case in the error handler. If the error from the high accuracy query is timeout, then try it again with enableHighAccuracy: false.

Have a look at this sample code.

You should also note that when testing this on a few devices, it returns location derived from WiFi even when enableHighAccuracy: true.

like image 36
Kevin Avatar answered Oct 22 '22 22:10

Kevin