[Solved] Feching user’s current location tips [closed]


So you really have two options.

The first, and most likely to be accurate, is to ask the user through their browser via javascript, what their location is. HTML5 allows you to do this through geolocation:

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation

The basics are as follows:

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

Obviously, this requires the user to agree to sharing their location with you. Which brings us to option 2 … approximate location by IP.

From the users IP you can use one of a number of services to translate an IP address to a physical location. The results can be somewhat varied though, and if the user is using any kind of VPN, proxy, etc, then you can be completely incorrect with your guess of where they actually are.

Here’s a free API you could start off with – https://freegeoip.net/

solved Feching user’s current location tips [closed]