I'm working on a project that would benefit from being able to get a user's geolocation (latitude and longitude) from the device's GPS radio.
Naïvely adding some geolocation code to the loader for the relevant route won't work because Remix is rendering the page server-side (which is what I want), where, obviously, the geolocation code makes no sense. (Even if the server supported geolocation, I want the user's location not the server's.)
How do I implement this?
So far I've borrowed from this answer on rendering something client-side.
This kind of works, in the sense my code is executing client-side, but it still doesn't quite work. In practice, the page briefly shows "Geolocation is not supported by this browser", then the <div>
disappears.
Once I can reassure myself that I can get the lat/long data and show it on the page, I assume it shouldn't be that hard to trigger something that reloads the page with the lat/long as parameters, and I can do my geolocation lookup.
I'm very much not a JavaScript expert, so forgive any neophyte mistakes or misapprehensions.
I've written this component:
const MyComponent = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
return <div>Geolocation is not supported by this browser</div>
}
function showPosition(position) {
return <div>Latitude { position.coords.latitude }, Longitude: { position.coords.longitude }</div>
}
}
Which I'm then inserting into the page like so:
<Suspense fallback={<div>Loading</div>}>
{ MyComponent() }
</Suspense>