1709054908

For beginner, geolocation with html5 👊🏻


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. use this code inside the `<body>` tags and see the result in your browser. ```js if(typeof navigator.geolocation == 'undefined'); alert("geolocation not supported."); else navigator.geolocation.getCurrentPosition(granted, denied); function granted(postion){ var lat = position.coords.latitude; var lon = position.coords.longitude; alert("permission granted. you are at location:\n\n" + lat + ", " + lon + "\n\nClick 'OK' to load google 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'; break; case 3: message = 'operatin timed out'; break; case 4: message = 'unknown error'; break; } alert("geolocation error: "+ message); } ``` Use it and apply it to your applications and see how it works. leave a comment in the comments to let us know if the code worked or if you got any errors.

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]