Simple JS/jQuery Geolocation Example

in #js8 years ago

Create new HTML document
Add two tags with classes .pos-latitude-value and .pos-longitude-value
Add jQuery (tested on 1.9)

js code:

(function($) {
    var k = 0;
    function showPosition(position) {
        $('.pos-latitude-value').html(position.coords.latitude);
        $('.pos-longitude-value').html(position.coords.longitude);
        k++;
        console.log('Checked ' + k + ' times');
        $('.status').html('Checked ' + k + ' times');
    }

    function getPosition(){
        navigator.geolocation.getCurrentPosition(showPosition);
    }
    getPosition();
    setInterval(getPosition, 1000);
})(jQuery);