Reputation: 47
I'm creating an app where i need to get access to user's coordinates to display their location on the map. I'm using react for that matter and the problem is the success callback never gets called and I can't get access to the coordinates. does anyone know what might cause this behavior? here's my code.
const Routing = (props) => {
const success = (position) => {
console.log(position);
};
useEffect(() => {
const geo = navigator.geolocation;
if (!geo) {
return;
}
geo.getCurrentPosition(success);
}, []);
return (
<div className="map">
<MapContainer />
</div>
);
};
thanks.
Upvotes: 1
Views: 695
Reputation: 13181
navigator.geolocation
has multiple drawbacks: it is not always available; it requires the user's consent; while doing so it displays an annoying pop-up; it uses approximation methods that are inconsistent and may give unexpected results; it required HTTPS, so it's hard to test on your local development server.
Do you have the possibility to test an alternative, see if it works for you and if you don't have error anymore? I suggest a realtime API such as Abstract IP Location which is free.
You call the API omitting the ip_address
parameter, so it returns the geolocation for the device making the API call. In the response, you get the longitude
and latitude
fields you can use to display the map's marker.
Upvotes: 1
Reputation: 30995
Maybe are you facing to an error ?
geo.getCurrentPosition(success, () => console.log('Error happened !'));
Upvotes: 0