var latitude, longitude, accuracy; function setGeolocation() { var geolocation = window.navigator.geolocation.watchPosition( function ( position ) { latitude = position.coords.latitude; longitude = position.coords.longitude; accuracy = position.coords.accuracy; document.getElementById( 'result' ).innerHTML += 'lat: ' + latitude + ', ' + 'lng: ' + longitude + ', ' + 'accuracy: ' + accuracy + '<br />'; }, function () { /*error*/ }, { maximumAge: 250, enableHighAccuracy: true } ); window.setTimeout( function () { window.navigator.geolocation.clearWatch( geolocation ) }, 5000 //stop checking after 5 seconds ); }; setGeolocation(); window.setInterval( function () { setGeolocation(); }, 15000 //check every 15 seconds );
<div id="result"></div> <script> /* by: thinkingstiff.com license: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ */ var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-23915674-6']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); var headerUri = 'http://stackoverflow.com/a/8720544/918414', headerCaption = 'geolocation.watchPosition breaks geolocation.getCurrentPosition'; document.body.insertAdjacentHTML( 'afterBegin', '<a href="' + headerUri + '" ' + 'target="_top" ' + 'onmouseover="this.style.opacity=\'.95\'" ' + 'onmouseout="this.style.opacity=\'1\'" ' + 'style="' + 'background-color: black;' + 'background-image: linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -webkit-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -moz-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -o-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -ms-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'border: 1px solid black;' + 'border-radius: 2px;' + 'color: white;' + 'display: block;' + 'font: normal 15px/26px Helvetica, Verdana, Tahoma;' + 'height: 26px;' + 'margin: -8px 0 8px -8px;' + 'opacity: 1;' + 'overflow: hidden;' + 'padding: 2px 8px;' + 'text-decoration: none;' + 'text-overflow: ellipsis;' + 'text-shadow: -1px -1px black;' + 'width: 99.5%;' + 'white-space: nowrap;' + '"><img ' + 'style="' + 'display: block;' + 'float: left;' + 'margin-right: 8px;" ' + 'src="http://thinkingstiff.com/images/stackoverflow.png" />' + headerCaption + '</a>' ); document.body.insertAdjacentHTML( 'afterBegin', '<a href="http://thinkingstiff.com" ' + 'target="_top" ' + 'onmouseover="this.style.opacity=\'.95\'" ' + 'onmouseout="this.style.opacity=\'1\'" ' + 'style="' + 'background-color: black;' + 'background-image: linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -webkit-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -moz-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -o-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'background-image: -ms-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );' + 'border: 1px solid black;' + 'border-radius: 2px;' + 'bottom: 0;' + 'color: white;' + 'display: block;' + 'font-family: Helvetica, Verdana, Tahoma;' + 'opacity: 1;' + 'padding: 4px 8px;' + 'position: fixed;' + 'right: 0;' + 'text-decoration: none;' + 'text-shadow: -1px -1px black;' + '">thinkingstiff.com</a>' ); </script>