Can I read the "real" mobile GPS coordinates from the site?

I made a website and I want to read the β€œreal” GPS position on mobile devices (Android and iPhone). When I try to set the location on my website from my Android using W3C , the javascript GPS method is not turned on and the position is set by IP (when I try the Google Maps application, the GPS is turned on and blinks in the status bar). Is there a way to read GPS (real GPS) from the Internet on a mobile phone? Thanks in advance!

+6
source share
2 answers

With HTML5 you can do this. You need to check the geolocation API: HTML5 Dive and W3C Geolocation API Specification

The simplest example looks like this:

if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function (position) { do_something(position.coords.latitude,position.coords.longitude); }, function (error){ switch(error.code){ case error.TIMEOUT: // Timeout break; case error.POSITION_UNAVAILABLE: // Position unavailable break; case error.PERMISSION_DENIED: // Permission denied break; case error.UNKNOWN_ERROR: // Unknown error break; default: break; } } ); } 

You are probably also interested in some specific browser implementations: Mozilla , IE , Chrome

Updated. As Mozilla says here , GPS devices, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or Wi-Fi) can be returned to getCurrentPosition () to run.

So, if you need high accuracy (for example, only GPS), use watchPosition instead of getCurrentPosition .

+7
source

As stated in the W3C specification, you may need to use the third argument of both watchPosition and getCurrentPosition, which is the PositionOptions interface to set enableHighAccuracy - true, otherwise, using the default value of false, GPS will not necessarily be enabled by the device.

0
source

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


All Articles