codegurukool.com · Free coding tutorials & online compiler

Home › HTML Tutorial › Geolocation API

Geolocation API

Ask for the visitor’s position.

navigator.geolocation.getCurrentPosition needs permission and a secure page. It is often blocked inside this preview, so handle the error.

Example code

<p id='o'>Asking...</p>
<script>
if(navigator.geolocation)navigator.geolocation.getCurrentPosition(
 p=>o.textContent=p.coords.latitude+', '+p.coords.longitude,
 e=>o.textContent='Not available: '+e.message);
else o.textContent='Unsupported';
</script>'

Run this code in the online editor

← HTML YouTubeDrag and drop API →