Get Gps Location from the Web Browser

Ways to get location of the client from the browser?

The user can be located by using following ways:-

  1. Using the IP addr.
    easiest to achieve but most unreliable due to the uncertainty of network addresses /
    topologies.
  2. Using the latitude, longitude pair
    most accurate solution, but most of the end users don’t know about their latitude,
    longitude value.
  3. Using the zipcodes
    nice to think but quite difficult to achieve, although ZipCodes are being used for

    quite a long time but this is not a standard yet. (see link

    http://en.wikipedia.org/wiki/Postal_code ). ZipCode alone is not sufficient for
    calculation of distance we need to convert it to latitude, longitude pair which is an

    overhead.
  4. Using user’s address
    most compelling thought since each user will have at least one geographic address.
    Address alone is not sufficient for calculation of distance we need to convert it to
    latitude, longitude pair which is an overhead.

The Best option will be to use options 2,3.4 together, You can do that in two steps:-

  1. First determine the latitude, longitude from the address.
  2. Then use this lat, long value for further processing i.e. storing in
    database etc.

For various options available for this see the answer of this question

How does a browser get GPS data from a computer?

Chrome and Firefox:

  • GPS via gpsd if available (Linux only) 1, 2
  • system's Location API (Windows 7 only) 2
  • WiFi networks via Google Location Services 1

Web-based browser GPS location

Please check the geolocation API

Determining the location of mobile web site user

You can use the HTML5 Geolocation API on devices that support it. It will return the most accurate location that is available which might be A-GPS or wifi location.

See for example this tutorial on how to use geolocation with JavaScript.

Basically you test for geolocation support and request the position:

if (navigator.geolocation) { // device can return its location
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
});
}

Please note, that this code will prompt the user if they want to share their current location or not.

Access Browser location

To get the browser to ask the user for geolocation you must use javascript.

This snippet from w3schools demonstrates this.

The latitude and longitude can you then pass forward to the backend.

<!DOCTYPE html>
<html>
<body>

<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);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}

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

</body>
</html>


Related Topics



Leave a reply



Submit