Ways to get location of the client from the browser?
The user can be located by using following ways:-
- Using the IP addr.
easiest to achieve but most unreliable due to the uncertainty of network addresses /
topologies. - Using the latitude, longitude pair
most accurate solution, but most of the end users don’t know about their latitude,
longitude value. - 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. - 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:-
- First determine the latitude, longitude from the address.
- 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
This' Different Between Repl and Script
iOS Uiwebview JavaScript - Insert Data -Receive Callbacks
How to Use Ranges in a Switch Case Statement Using JavaScript
$.Deferred: How to Detect When Every Promise Has Been Executed
How to Extract Text from a PDF in JavaScript
Format a Number as 2.5K If a Thousand or More, Otherwise 900
How to Improve Performance of Ngrepeat Over a Huge Dataset (Angular.Js)
How Does JavaScript Determine the Number of Digits to Produce When Formatting Floating-Point Values
Most Efficient Method of Detecting/Monitoring Dom Changes
How to Create an Object from an Array of Key-Value Pairs
Valueof() VS. Tostring() in JavaScript
JavaScript Function Leading Bang ! Syntax
Retrieve Dom Target from Drag Callback When 'This' Is Not Available
Why How to Not Define Functions in Jquery's Document.Ready()
How to Find All Subsets of a Set in JavaScript? (Powerset of Array)