I'm thinking of building a new website and part of its functionality will be the geolocation function, so here's a brief introduction to how we can add it to our websites. Using geolocation, your browser can return information to a web server about your location. This information can come from a GPS chip in the computer or mobile device you’re using, from your IP address, or from analysis of nearby WiFi hotspots. For security purposes, the user is always in control and can refuse to provide this information on a one-off basis or can enable settings to either permanently block or allow access to this data from one or all websites. ## <br>Let's go to the code now --- ```html <html> <head> <title>Geolocation Example</title> </head> <body> <script> if(typeof navigator.geolocation == 'undefined') alert('Geolocation not supported.'); else navigator.geolocation.getCurrentPosition(granted, denied); function granted(position){ var lat = position.coords.latitude var lon = position.coords.longitude alert('Permission Granted. You are at lcation:\n\n' + lat + ', ' + lon '\n\nClick "OK" to load Goolge Maps with your location'); window.location.replace("https://www.google.com/maps/@" + lat + ', ' + lon + ',14z'); } function denied(error) { var message switch(error.code){ case 1: message = 'Permission Denied'; break; case 2: message = 'Position Unavailable'; breaak; case 3: message = 'Operation Timed Out'; break; case 4: message = 'Unknown Error'; break; } alert('Geolocation Error: + message'); } </script> </body> </html> ``` This example simply shows how we can add geolocation to our website in just a few codes. If you like content like this, help our community grow by participating in the site. Create your account today