HTML5: Get Current Position

(function($) {
	 
    var methods = {
        current : function()
        {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(position, error);
            }

            function position(pos) {
                var latitude = pos.coords.latitude;
                var longitude = pos.coords.longitude;

                alert('Latitude: ' + latitude + ', Longitude: ' + longitude);
            }

            function error(err) {
                alert('ERROR(' + err.code + '): ' + err.message);
            };
        },
    };
     
    $.fn.Location = function(method) {
        if (methods[method])
        {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || !method)
        {
            return methods.init.apply(this, arguments);
        }
        else
        {
            $.error('Method ' + method + ' does not exist on jQuery.Ika');
        }
    };
     
})(jQuery);

$(document).ready(function(){
    $(document).Location('current');
});

Refererences:

  1. https://developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition
  2. http://dev.w3.org/geo/api/spec-source.html

One thought on “HTML5: Get Current Position

Leave a comment