Created
November 24, 2020 08:02
-
-
Save codebucks27/6a96b155e6dd9668d790279ed3a7f71f to your computer and use it in GitHub Desktop.
Medium Article GeoLocation --2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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