3

I want to get the most accurate position of my mobile phone with android. I'm comparing accuracy value in 2 seconds interval and if value is less than last value I'm trying to keep the position in <p>. It looks fine but if accuracy of gps is less than 10 or 11 id doesn't save. I know that accuracy of phone gps is not big but it looks like a javascript bug. Any help? Here is my basic concept.

check this link in your mobile phone browser(firefox)

and code

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button id="measure">measure1</button>

<button id="start">start</button>

<button id="stop">stop</button>

<p id="demo">x</p>
<p id="demo2">y</p>

<input type="text" id="demo3">reference accuracy <br>
<input type="text" id="demo4">last accuracy <br>
<P id="demo5"></p> 
<script>
var x = document.getElementById("demo");
var y = document.getElementById("demo2");
var z = document.getElementById("demo3");
var q = document.getElementById("demo4");
var q2 = document.getElementById("demo5");


        document.getElementById("measure").onclick = getLocation;

        document.getElementById("start").onclick = clockStart;  
        document.getElementById("stop").onclick = clockStop;    

        var timerId // current timer if started

        function clockStart() {  
          if (timerId) return
          getLocation()
          timerId = setInterval(getCorrection, 2000)

        }

        function clockStop() {
          clearInterval(timerId)
          timerId = null
        }


  function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition, showError, {enableHighAccuracy: true});
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            } 
        }


   function getCorrection() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition2, showError, {enableHighAccuracy: true});
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            } 
        }       

        function showPosition(position) {   


                x.innerHTML = "Latitude: " + position.coords.latitude +
                    "<br>Longitude: " + position.coords.longitude +
                    "<br>Accuracy: " + position.coords.accuracy +
                    "<br>Speed: " + position.coords.speed + " m/s" +
                    "<br>Altitude: " + position.coords.altitude +
                    "<br>altitudeAccuracy: " + position.coords.altitudeAccuracy +
                    "<br>Headin: " + position.coords.heading +
                    "<br>Timestamp: " + position.timestamp  ;

                z.value = position.coords.accuracy;


        }

          function showPosition2(position) {   


                y.innerHTML = "Latitude: " + position.coords.latitude +
                    "<br>Longitude: " + position.coords.longitude +
                    "<br>Accuracy: " + position.coords.accuracy +
                    "<br>Speed: " + position.coords.speed + " m/s" +
                    "<br>Altitude: " + position.coords.altitude +
                    "<br>altitudeAccuracy: " + position.coords.altitudeAccuracy +
                    "<br>Headin: " + position.coords.heading +
                    "<br>Timestamp: " + position.timestamp  ;
           //     window.document.body.onload = showPosition;  
                q.value = position.coords.accuracy;



            if ( z.value > q.value){z.value = position.coords.accuracy; 
                                    q2.innerHTML = position.coords.accuracy + " " +  position.coords.latitude + ' ' + position.coords.longitude + " the best accurancy with positon";
                                    console.log('better')}


            else (z.value < q.value); { console.log('worse')}
        }


        function showError(error) {
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    x.innerHTML = "Uşytkownik nie zezwolił na geolokalizację?"
                    break;
                case error.POSITION_UNAVAILABLE:
                    x.innerHTML = "Informacja o lokalizacji jest niedostępna"
                    break;
                case error.TIMEOUT:
                    x.innerHTML = "Przekroczono czas zapytania"
                    break;
                case error.UNKNOWN_ERROR:
                    x.innerHTML = "Wystąpił nieznany błąd"

            }
        }



</script>

</body>
</html>
  • @Marcel Kohls says "try to compare values with parseFloat() or parseInt() functions, like: if( parseFloat(z.value) > parseFloat(q.value) ). This way you will be sure that js will be comparing numbers and not strings. I am saying this because on a string comparation, "2" is bigger than "10"." and it works :D – pawaelus Apr 29 '16 at 6:57
2

Sadly, you can't.
The HTML5 Geolocation API works by using the browser implementation of the spec. Browsers like Chrome will send data to Google servers in order to try to improve the location(Network information, like your IP, as described here), however, that doesn't always happen nor is guaranteed to, the browser/OS may choose what it think is the best, be it GPS sensor, network triangulation or even some plataform specific trickery like Bluetooth positioning.
You can always try making requests to location providers(There's a ton on Google), tho, it'll probably lead to poor results compared to the standard API. Another option would be comparing your results with the Generic Sensor API, that can potentially give information about the device acceleration and movement.

If positioning is REALLY crucial to your application, I suggest that you try some native development, there, you can use sensors and OS specific calls more efficiently. This is also more evident since different browser implementations can lead to different user results, not to mention the incompatibility (The generic sensor API is an example of poor compatibility).

In the future, there's an extension of the Generic Sensor API that'll expose more position related sensors, you can read more of the draft here.

  • Thank for your help :D – pawaelus Jun 17 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.