HTML5: Getting Geo-location using geolocation and JavaScript

In html5 you can get your device geo-location( latitude and longitude coordinates) using HTML5.

Syntax for method
geolocation.getCurrentPosition(successCallback [, errorCallback] [, options])

In above method "geolocation" object you can get by window.nevigation.geolocation object.
getCurrentPosition is method for getting device location
successCallback is method to handle callback from geolocation service
errorCallback and option are optional parameters.

options are different options you can give in JSON object notation form

A boolean value true/false to specify accuracy of location, but it can slow down the performance

An Integer value that indicates the time, in milliseconds, allowed for obtaining the position.
If timeout is Infinity, (the default value) the location request will not time out.
If timeout is zero (0) or negative, the results depend on the behavior of the location provider.

An int value indicates age of cached value in milliseconds. If this value is zero then getCurrentPosition will try to get updated data.

Here is one java script sample for calling geolocation api

    function getPosition() {
        if (nav == null) {
            nav = window.navigator;
        if (nav != null) {
            var geoloc = nav.geolocation;
            if (geoloc != null) {
            else {
                alert("geolocation not supported");
        else {
            alert("Navigator not found");
    function posCallback(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
position.coords contains latitude, longitude and other attributes like accuracy altitude  etc.
blog comments powered by Disqus