DEV Community

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

Collapse
 
arjunkumardev profile image
ArjunKumarDev

Is this code will work on Safari browser?!? @codebucks

Collapse
 
codebucks profile image
CodeBucks

Based on this website I guess you can use it.

Collapse
 
arjunkumardev profile image
ArjunKumarDev

I have tried it,but it doesn't work...

Thread Thread
 
codebucks profile image
CodeBucks

I guess navigator.permissions is not supported in safari.

Collapse
 
chattes profile image
Sourav Chatterjee

In safari , we can call
navigator.geolocation.getCurrentPosition(success, errors, options).
Tested and working in mac.

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}}