Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.geolocation.watchPosition just not working

Okay, my code is below so I'll explain what I'm doing and what I'm getting for results.

I'm trying to grab the users location using navigator.geolocation.watchPosition. I've specified both a success and error callback. The first time I get the location it's usually very inaccurate so I use watchPosition to keep getting the location until it's either tried 4 times or got a location that is accurate to within 500 metres.

Problems I am having:

  • It NEVER enters the error callback for android (tested on HTC Sensation and Samsung Galaxy S3). On the iPhone however, it seems to enter the error callback over 50% of the time.

  • It also never timesout. It just keeps searching until it's received a location.

  • It takes anywhere between 0-60 seconds to retrieve a location. Sometimes it has it before the page is done loading, other times it takes the better part of a minute. There's also no discernable pattern as to why it gets it fast sometimes and why not other times.

I've searched many a forum post and many a stackoverflow question as well as the spec for geolocation and any other docs out there. There's not very much information on it, let alone these problems. Is there any I'm doing wrong? Or is this just the nature of working with location?

if(navigator.geolocation){
        var geo_count = 0;

        var wpid = navigator.geolocation.watchPosition(success, error, {
            enableHighAccuracy: true, 
            maximumAge: 0, 
            timeout: 10000
        });

        function success(position){
            alert(position.coords.accuracy)

            geo_count = geo_count + 1;

            if(position.coords.latitude && position.coords.longitude && position.coords.accuracy < 500){
                navigator.geolocation.clearWatch(wpid); 

                alert('position obtained!');    
            }
            else if(geo_count > 4){
                navigator.geolocation.clearWatch(wpid);

                alert('inaccurate position obtained');
            }
        }

        function error(error){
            switch(error.code){
                case 1:
                    alert('permission denied');
                    break;
                case 2:
                    alert('position unavailable');
                    break;
                case 3:
                    alert('timeout');
                    break;
                default:
                    alert('unknown error');
                    break;
            }
        }
    }
like image 538
jasonaburton Avatar asked Nov 28 '12 17:11

jasonaburton


People also ask

Why is geolocation not working?

If you are facing problems with geolocation on your website running our WordPress Directory theme using Google Chrome browser, it's because of Google's decision to allow geolocation functionality only for websites that use SSL certificates.

How does geolocation watchPosition work?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.

Which method is used to stop the watchPosition method?

The watchPosition method returns a unique transaction ID (number) associated with the asynchronous call. Use this ID to cancel the watchPosition call and to stop receiving location updates.


1 Answers

I recommend to check using:

if (typeof navigator.geolocation === 'object' && typeof navigator.geolocation.watchPosition === 'function') {
    ...
}

"The Geolocation.watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function."

Maby it doesn't trigger because the position is still the same? ^^

Try to debug it using Geolocation.getCurrentPosition() with an Interval or Webworker: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation

Good luck

like image 64
redaxmedia Avatar answered Nov 15 '22 18:11

redaxmedia