Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the simplest way to get a user's latitude and longitude from an ip address [closed]

Tags:

geolocation

I always end up needing the user's location on most web projects that I work on. I've used Maxmind's GeoIp, but it involves you importing their dataset and it needs constant updating. There are also other free and paid services, but I just wanted something simple that I could add to any site in a matter of seconds.

So this is sort of baited because I have a simple solution, detailed below, but I wanted to see if anyone else uses this technique and if there are any better solutions or if there are some unforeseen pitfalls.

like image 969
Greg Roberts Avatar asked Oct 16 '09 06:10

Greg Roberts


People also ask

Can you get latitude and longitude from IP address?

By geographically mapping the IP address, it provides you with location information such as the country, state, city, zip code, latitude/longitude, ISP, area code, and other information.

Can you find someone's exact location from IP address?

IP addresses do reveal your geolocation, but not your precise location like a home address does. IP addresses will also never reveal your name, phone number, or other precise personal information.

How do I find the geo location of an IP address?

Just send HTTP GET request to https://api.ipgeolocationapi.com with different parameters and get the JSON result. For example, the following HTTP GET request will give you a location of the specified IP address. The following request returns the country information specified by the ISO country code.


1 Answers

The implementation I currently use I've seen in a couple other questions, but I haven't seen it as the selected answer.

I've detailed this in my blog thenullreference.com, but here it is in short:


Essentially all you need to do is load Google's API loader script:

<script type="text/javascript" src="http(s)://www.google.com/jsapi"></script>

Then you have access to several properties that give you detailed location info.

The object you want to look at is google.loader.ClientLocation

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code - ISO 3166-1 country code
  • ClientLocation.address.region - country specific region in US this is state

*Note some of these can be null

For more info on this API check out here

Does anyone else use this? Does anyone have something that they use that is as simple and better? Are there issues with this approach?

I'm unaware of the coverage/accuracy of this solution, but I would think that Google keeps it updated and is probably pretty good.

like image 81
Greg Roberts Avatar answered Oct 24 '22 00:10

Greg Roberts