Getting your location with HTML5 geolocation

The HTML5 specification includes support for geolocation for determining an approximation of your position. JavaScript is used in this example to get and display your position.

<!DOCTYPE html>

<html>
<head>
    <title>Geolocation with HTML5</title>

    <script type="text/javascript">
        function getMyLocation(){
            if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(function(location){
                    var myLocation = document.getElementById('myLocation');
                    myLocation.innerHTML = 'Latitude: ' + location.coords.latitude +
                                            ' Longitude: ' + location.coords.longitude;
                });
            }
        }
    </script>

</head>

<body onload="getMyLocation();">

<div id="myLocation">Fetching your location...</div>

</body>
</html>

Output:

Geolocation

 

This entry was posted in JavaScript and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?