DEV Community

Discussion on: Ways to get user's geo location

Collapse
 
steinbring profile image
Joe Steinbring

I had to deal with something like this recently. I was looking to sort a list of physical locations by how far they are from the user. Since I had the latitude and longitude of each of the physical locations and the latitude and longitude of the user, I could use the Haversine formula to calculate the distance between the user and each of the locations (in kilometers). I settled on doing IP-based geolocation when the page loads but then using navigator.geolocation to update the coordinates if the user clicks a "find my location" button. The only trick left is to show the user's location (I just did City and State) for each method. If they are on a cellphone or using a VPN, their location could be showing up as being in a completely different part of the country and I figured that explicitly saying where I thought they were would solve that.

I think that I have convinced myself that this hybrid approach is the best option.