How to get the GPS location of who access the website?

Asked

Viewed 2,238 times

3

Hello I need to develop a website that creates routes, I’m using Google API’s Javascript. I need to get the user’s location, but the same should be the GPS if the user is accessing through a mobile device, and not by IP as the HTML5 function.

Is it possible to access GPS data through a web application? How do I develop this?

1 answer

4

You can’t access a device’s hardware settings from the browser. The most you can do is use the HTML5 Geolocation, upon the user’s acceptance, to obtain the user’s location coordinates.

As can be seen in the Mozilla documentation, the Geolocation is not based, alone, on IP. If the device has a GPS, it will use the GPS to calculate the location, which can cause a longer delay to obtain the data.

Below is an example of how to use Geolocation, taken from w3School.

<html><head></head><body contenteditable="false">

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

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

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

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}
</script>



</body></html>

Now, if you really want to use your device’s GPS, you’ll need to develop an app and ask the user for permission first. This permission is set in the APP.

To see how it works visit the example site, because there are blocks of secure connections (HTTPS).

  • But with HTML5 the location will be through the IP. which cannot be used in this application as a relatively large location error occurs. In addition google Chrome is not even displaying the message to allow sharing the location. Even so thank you.

  • 2

    @Eliardomarini The problem is that you cannot do what you want without developing an app for this purpose. Other, Chrome (as of version 50) accepts use through trusted addresses, that is, addresses with HTTPS and not HTTP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.