Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

getnathan's avatar

Geo location of the users

Is there API of Laravel to determine the geographical location of website visitors ??

0 likes
30 replies
willvincent's avatar

You really felt it necessary to revive a 6yr old thread to try and put me in my place? 🙄

Lighten Up Francis

getnathan's avatar

My idea is to connect to a local database and bring location specific data to the user. Backend is laravel and front end is angularjs.

So when user visits the page I need to know users geolocation lat and long data first. Using that lat and long data I am going to reverse geocode to find the user's postcode. Then I will connect the database to fetch the data related to that postcode.

Although modern browsers are capable of detecting geolocation lat and long data of incoming request I tend to hand over that task to backend; otherwise I have to wait until front end fetches user's gelocation and then give it to back end. If there is a way to send the user's geolocation with the initial request then I can access it from a superglobal varaible at the back end and fetch the data accordingly. But, I don;t think it is possible. So I think letting backend to do the whole job makes an easy process. Since Laravel request starts at the routes.php I can initiate whole process right there without waiting until the front end browser picks the geolocation and send that data to back end. In my approach I will have to use user's IP address to find the user's location though.

I would love to hear your thoughts on this.

getnathan's avatar

When I try to install Torann/laravel-geoip it throws an error message saying .... Installing dependencies (including require-dev) from lock file Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them. Nothing to install or update Generating autoload files Generating optimized class loader

And after that couldn't follow rest of the process since it gave me errors

I will try to copy across manually.

sitesense's avatar

'I would love to hear your thoughts on this.'

I did something very similar to that in my old project. In a future project that I have in mind, I'll be thinking about using both the backend and frontend approach. Backend to get a rough location and frontend to pinpoint as close as possible.

Especially with mobile devices, if you need more accuracy of the user's current, actual location, you can't be relying on ip address alone.

bashy's avatar

Postcode will only be based on the IP, not the user's location. You know that?

getnathan's avatar

@sitesense thanks for your thoughts. @bashy - really I didn't know that. I am glad you told me it.

@sitesense, according to @bashy, no point of using geolocation using front end isn't it? Please correct me if I am wrong. Since post code is based on IP address (not the user location) it may be a good idea to use GeoIP (https://github.com/Torann/laravel-geoip) as it utilises IP address as my whole aim is to get the user;s postcode.

By the way you guys are amazing... and thanks lot for your inputs. It gave me really good start on this.

getnathan's avatar

By the way the error message I got when I try to install GeoIP was related to my broken composer lock file. So I deleted vendor folder and composer lock file and called composer install and then installed GeoIP successfully after that.

sitesense's avatar

@getnathan of course it depends how accurate you need to be :)

IP addresses and their relation to a particular latitude/longitude, are guesswork in some cases and not always that accurate. Saying that, I've had decent results from the MaxMind database.

Edit:

As an example, a known user in France connecting via Orange broadband had dynamic IP addresses that would change every few days. This user could be reported as being in different cities 100's of miles apart.

bashy's avatar

You can get close, sure. As sitesense said, it's really down to what you're using it for. If you were using it to generate a postcode for delivery... that wouldn't be a good idea :P

getnathan's avatar

@sitesense and @ bashy sorry I forgot to tell you that it has to be 100% accurate.
I need to find a way to get exact postcode of user with 100% accuracy. I used GeoIP (https://github.com/Torann/laravel-geoip) few mintes ago and it gave me wrong post code for my IP. I used MaxMind db. I live in Melbourne Australia.

sitesense's avatar

@getnathan as another example, go to this site but ignore the request for "computer's location" (don't allow):

http://whatismyipaddress.com/

On the map, "click for more details". You should now see your postcode in the new page. Mine is wrong.

Now below the map on this page, click "Update IP Location" and allow the browser to determine your location.

You'll be presented with another map with other locations by different "Providers" listed below it. Check out the W3C location and this is probably far more accurate than the others.

The W3C Geolocation service determines location by the browser providing GPS location (if available) and signal strengths of visible WiFi annoucements.

getnathan's avatar

@sitesense thanks for the link. All of you guys gave me amazing info and I should thank all of you immensely for that.

getnathan's avatar

@sitesense I went back and checked it agian and I found that you are correct as W3C's location data was pretty accurate. But according to @bashy I still have to go with my IP address to get post code. isnt' it? Because I have to find a way to convert ip address or lat/long to postcode.

sitesense's avatar

@getnathan no, @bashy was pretty much saying the opposite. He means the IP address is almost useless to determine location with any accuracy.

If you have the lat/lng, you can find the postcode in many ways. Probably the easiest is with the Google Geocoding Api:

https://developers.google.com/maps/documentation/geocoding/#geocoding

Edit: This is what can be achieved - enter your lat/lng into this site - it gave my next door neighbour's full address!

http://www.doogal.co.uk/LatLong.php

willvincent's avatar

Geo Location by my IP address places me 60 miles away from my actual location. If I allow Google to access my location, and try to pull it up on the map, on my laptop, it places me within a few hundred yards of my actual location. Presumably this is because it's based on my mobile phone location:

If you have Location History turned on, your location may be based on the location of your mobile device.

At any rate, geolocation by ip is not terribly reliable or accurate.

devdict's avatar

Thank you. Is this location comes from the client site?

Please or to participate in this conversation.