What is the best template to get geolocation from JavaScript with high precision?

I played with the geolocation JavaScript API (mainly on iOS 6 and Android Jellybean devices) and despite the transfer:

{ enableHighAccuracy: true, maximumAge: 0 } 

like PositionOptions, the picture seems that with the first answer I get a fast response of low accuracy ( location.coords.accuracy reached 16 130), and in the second or third answer I get a high-precision answer (with location.coords.accuracy value up to 31). Now I am making a setTimeout call (with a delay of 3 seconds) to request GPS twice, and I am ignoring the first answer. But I'm wondering if anyone has any pointers to a better implementation. Here is my code (with a 3 second timeout) (and a demo , you'll need a console)

 var check = function() { if ("geolocation" in navigator) { console.log('looking'); navigator.geolocation.getCurrentPosition(function(location) { console.log('Ignoring the following data:'); console.log({ lat: location.coords.latitude, lng: location.coords.longitude, accuracy: location.coords.accuracy}); setTimeout(function() { navigator.geolocation.getCurrentPosition(function(location) { console.log('On my system I am firing a Lucene Spatial search with the following data:'); console.log({ lat: location.coords.latitude, lng: location.coords.longitude, accuracy: location.coords.accuracy}); }, function() { console.log('navigator error occurred on second call... weird'); }, { enableHighAccuracy: true, maximumAge: 0 }); }, 3000); }, function() { console.log('navigator error occurred on first call.'); }, { enableHighAccuracy: true, maximumAge: 0 }); } else { console.log('geolocation not available'); } } 
+4
source share
1 answer

If your application requires such precise geolocation, perhaps you can determine what is the critical threshold for the accuracy property and set up a listener that monitors changes in accuracy and only responds when the answer is accurate enough.

0
source

Source: https://habr.com/ru/post/1489339/


All Articles