DEV Community

Discussion on: How to get user’s location with React JS

Collapse
 
shankar0295 profile image
Shankar

navigator.permissions not supported in safari. Try this instead

Inside function
const location = () => {
if ('geolocation' in navigator) {
/* geolocation is available /
navigator.geolocation.getCurrentPosition(success)
} else {
/
geolocation IS NOT available */
navigator.geolocation.getCurrentPosition(errors)
}
}

const success = async (pos) => {
    var crd = pos.coords;
    const res = await fetch(api)
    const location = await res.json();
    setUserLocation(location)
}

const errors = (err) => {
    alert(`ERROR(${err.code}): ${err.message}`);
}
Enter fullscreen mode Exit fullscreen mode

In jsx,
{!userlocation ? "India" : ${userlocation[0].name},${userlocation[0].country}}