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

3

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

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

    
asked by anonymous 14.07.2016 / 17:40

1 answer

4

You can not access the hardware settings of a device through the browser. The best you can do is to use HTML5 Geolocation , by accepting the user, to get the location coordinates of the user.

As can be seen in the mozilla documentation , geolocation does not is based, in isolation, on IP. If the device has a GPS, it will use GPS to calculate the location, which can lead to a longer delay to get the data.

Below is an example of how to use Geolocation, taken from the 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 the device's GPS, you'll need to develop an application and request the user's permission first. This permission is set in APP.

  

To see the operation go to the sample site, because there are secure connection locks (HTTPS).

    
14.07.2016 / 18:00