Get GPS location from the web browser Get GPS location from the web browser javascript javascript

Get GPS location from the web browser


If you use the Geolocation API, it would be as simple as using the following code.

navigator.geolocation.getCurrentPosition(function(location) {  console.log(location.coords.latitude);  console.log(location.coords.longitude);  console.log(location.coords.accuracy);});

You may also be able to use Google's Client Location API.

This issue has been discussed in Is it possible to detect a mobile browser's GPS location? and Get position data from mobile browser. You can find more information there.


To give a bit more specific answer. HTML5 allows you to get the geo coordinates, and it does a pretty decent job. Overall the browser support for geolocation is pretty good, all major browsers except ie7 and ie8 (and opera mini). IE9 does the job but is the worst performer. Checkout caniuse.com:

http://caniuse.com/#search=geol

Also you need the approval of your user to access their location, so make sure you check for this and give some decent instructions in case it's turned off. Especially for Iphone turning permissions on for Safari is a bit cumbersome.


Use this, and you will find all informations at http://www.w3schools.com/html/html5_geolocation.asp

<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>