Skip to content

Instantly share code, notes, and snippets.

@codebucks27
Created November 24, 2020 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebucks27/6a96b155e6dd9668d790279ed3a7f71f to your computer and use it in GitHub Desktop.
Save codebucks27/6a96b155e6dd9668d790279ed3a7f71f to your computer and use it in GitHub Desktop.
Medium Article GeoLocation --2
import React, { Component } from "react";
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};
function success(pos) {
var crd = pos.coords;
console.log("Your current position is:");
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
}
function errors(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
export default class GeoLocation extends Component {
componentDidMount() {
if (navigator.geolocation) {
navigator.permissions
.query({ name: "geolocation" })
.then(function (result) {
if (result.state === "granted") {
console.log(result.state);
//If granted then you can directly call your function here
navigator.geolocation.getCurrentPosition(success);
} else if (result.state === "prompt") {
navigator.geolocation.getCurrentPosition(success, errors, options);
} else if (result.state === "denied") {
//If denied then you have to show instructions to enable location
}
result.onchange = function () {
console.log(result.state);
};
});
} else {
alert("Sorry Not available!");
}
}
render() {
return (
<div>
<h2>GeoLocation</h2>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment