Geolocation

Size
2,429 Kb
Views
26,312

How do I make an geolocation?

How to use navigator.geolocation to get the user's current latitude and longitude.. What is a geolocation? How do you make a geolocation? This script and codes were developed by Mobile Application Design-Development on 10 August 2022, Wednesday.

Geolocation Previews

Geolocation - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Geolocation</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="output">
</div>
<p><a href="">MDN Geolocation Reference</a></p>
<p>This page must run under https to work</p> <script src="js/index.js"></script>
</body>
</html>

Geolocation - Script Codes CSS Codes

div, p{ font-size: 24px; line-height: 1.6; padding: 0.5rem 2rem;
}

Geolocation - Script Codes JS Codes

document.addEventListener("DOMContentLoaded", getLocation);
document.body.addEventListener("click", getLocation);
function getLocation(){ if( navigator.geolocation ){ document.getElementById("output").innerHTML = ""; //code goes here to find position let to = 1000 * 30; //1000 times 30 = 30 seconds let max = 1000 * 60 * 60; //1000 * 60 * 60 = 1 hour var params = {enableHighAccuracy: false, timeout:to, maximumAge:max}; //enableHighAccuracy means try to use GPS and drain the battery //for improved accuracy within a few meters. //maximum age is how long to cache the location info //timeout is how long to wait for the network to respond after the user says ok navigator.geolocation.getCurrentPosition( reportPosition, gpsError, params ); //to continually check the position (in case it changes) use // navigator.geolocation.watchPosition( reportPosition, gpsError, params) }else{ //browser does not support geolocation api alert("Sorry, but your browser does not support location based awesomeness.") }
};
function reportPosition( position ){ var output = document.querySelector("#output"); output.innerHTML += "Latitude: " + position.coords.latitude + "&deg;<br/>" + "Longitude: " + position.coords.longitude + "&deg;<br/>" + "Accuracy: " + position.coords.accuracy + "m<br/>" + "Altitude: " + position.coords.altitude + " m<br/>" + "Heading: " + position.coords.heading + " &deg;<br/>" + "Speed: " + position.coords.speed + " m/s<br/>" + "Timestamp: " + position.timestamp;
}
function gpsError( error ){ var errors = { 1: 'Sorry your browser did not have permissions to get your location.', 2: 'Unable to determine your location.', 3: 'Location request took too long.' }; // errors[1] alert("Error: " + errors[error.code] + "... " + error.message );
}
Geolocation - Script Codes
Geolocation - Script Codes
Home Page Home
Developer Mobile Application Design-Development
Username mad-d
Uploaded August 10, 2022
Rating 3
Size 2,429 Kb
Views 26,312
Do you need developer help for Geolocation?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Mobile Application Design-Development (mad-d) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!