get current latitude and longitude from gps enabled device
Asked Answered
R

4

9

I would like to get the latitude and longitude of current location from gps enabled mobile device right from the web browser. May I know is this possible? how to do it? does it require geolocation api? Some coding example would be helpful. Thanks.

Ratha answered 10/8, 2010 at 18:35 Comment(4)
User must willfully provide this information. See the Google home page on mobile.Hypochlorite
Which device? Which API?Kabuki
@josh yes, of course it is assumed that user has approved to share location at the phone settings..Ratha
@bear any gps enabled device. any recommended api?Ratha
F
17

Use the HTML5 Geolocation API, here's the official spec and examples.

EDIT

I've updated my answer to include current browser support.

W3C Geolocation API support

Firefox 3.5+
Safari 5.0+
Chrome 5.0+
Opera
iPhone 3.0+
Android 2.0+

· ·
Other phones not listed above use Gears or their own, platform-specific APIs.

Ahh, will we ever have just one single API? :)

Many thanks to Mark Pilgrim for his awesome post.

Featurelength answered 10/8, 2010 at 19:26 Comment(5)
is it means that html5 geolocation api can be used by all gps enabled devices? thanks..Ratha
Mark Pilgrim's post you link to is no longer available :(Mystique
Google Gears is long deprecated.Chelyuskin
@BuhakeSindi this was answered 2.5 years ago.Featurelength
This does not work on mobile networks or Chrome (only https) or Safari. I use ipinfo.io which is ok for wifi connected devices but returns lat long of networks ISP.Stifling
R
9

Here is an actual JavaScript code which uses HTML5 Geolocation API. The following works on both Android browser and iPhone Safari:

            function onPositionUpdate(position)
            {
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                alert("Current position: " + lat + " " + lng);
            }

            if(navigator.geolocation)
                navigator.geolocation.getCurrentPosition(onPositionUpdate);
            else
                alert("navigator.geolocation is not available");
Roll answered 29/11, 2010 at 12:11 Comment(0)
P
0

You don’t have to have the newest mobile phone to use GPS and Geolocation API. Almost every mobile browser (without proxy server) can be used to read position from buidin GPS. If You have Java and GPS in Your phone – You can use mobile-gps-web-gate – see at http://code.google.com/p/mobile-gps-web-gate/

Pashm answered 20/11, 2010 at 16:0 Comment(0)
K
0

can be helpful in future for someone

<!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) {
 var a = position.coords.latitude + ',' + position.coords.longitude;

 alert(a);
}
</script>
</body>
</html>
Karolekarolina answered 3/1, 2021 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.