W3C Geolocation API: Difference between revisions

Content deleted Content added
cat
Line 27:
Simple [[JavaScript]] code that checks if the browser has the Geolocation API implemented and then uses it to get the current position of the device. this code creates a function which can be called on html using <code><body onload="geoFindMe()"></code>:
<source lang="javascript">
functionconst geoFindMe = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, geoOptions);
} else {
Line 35:
}
 
functionconst success = (position) => {
varconst latitude = position.coords.latitude;
varconst longitude = position.coords.longitude;
varconst altitude = position.coords.altitude;
varconst accuracy = position.coords.accuracy;
console.log("`lat: " + ${latitude + "} long: " + ${longitude}`);
}
 
functionconst error = (error) => {
console.log("`Unable to retrieve your ___location due to " + ${error.code + "}: " + ${error.message}`);
}
 
varconst geoOptions = {
enableHighAccuracy: true,
maximumAge: 30000,