Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

How to Find Current location based on IP-Address using Javascript?

4.81/5 (29 votes)
6 May 2011CPOL 237.1K  
Sometime we have requirement to get current location e.g. Country code, country name etc using lat long. Though there are lot of way to accomplish this task but i found the easiest way to do this. And definitely its worth sharing as a tip and tricks.

The following javascript code is used to find the location, latitude and longitude based on your IP address
XML
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

<br>Country Code:
<script language="JavaScript">document.write(geoip_country_code());</script>
<br>Country Name:
<script language="JavaScript">document.write(geoip_country_name());</script>
<br>City:
<script language="JavaScript">document.write(geoip_city());</script>
<br>Region:
<script language="JavaScript">document.write(geoip_region());</script>
<br>Region Name:
<script language="JavaScript">document.write(geoip_region_name());</script>
<br>Latitude:
<script language="JavaScript">document.write(geoip_latitude());</script>
<br>Longitude:
<script language="JavaScript">document.write(geoip_longitude());</script>
<br>Postal Code:
<script language="JavaScript">document.write(geoip_postal_code());</script>



It displays your city name, latitude, longitude,country etc.
I hope you will like it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)