Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.81/5 (29 votes)
6 May 2011CPOL 228.6K   12   33
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)


Written By
Technical Lead
United States United States
Rohit started Embedded Programing in his college days and now he is a Software Developer by Profession. Mainly he interested in cutting edge technology offer by Microsoft (i.e Azure,MVC). He Loves coding and his passion is always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books and hang out with friends is his most favorite past time hobby.


1. 20 Apr 2014: Best Mobile Article of March 2014 - First Prize

Comments and Discussions

 
Bugfor using a api Pin
Member 1206274616-Oct-15 19:49
Member 1206274616-Oct-15 19:49 
GeneralRe: for using a api Pin
maq_rohit2-Nov-15 9:22
professionalmaq_rohit2-Nov-15 9:22 
QuestionReferenceError: geoip_country_name is not defined Pin
Member 1163244921-Apr-15 23:53
Member 1163244921-Apr-15 23:53 
AnswerRe: ReferenceError: geoip_country_name is not defined Pin
maq_rohit5-May-15 13:50
professionalmaq_rohit5-May-15 13:50 
GeneralMy vote of 3 Pin
Anand_Mpec28-Dec-14 19:13
Anand_Mpec28-Dec-14 19:13 
GeneralRe: My vote of 3 Pin
maq_rohit5-May-15 13:55
professionalmaq_rohit5-May-15 13:55 
GeneralMy vote of 1 Pin
Dnyaneshwar@Pune22-Dec-14 22:43
Dnyaneshwar@Pune22-Dec-14 22:43 
GeneralRe: My vote of 1 Pin
maq_rohit31-Dec-14 11:03
professionalmaq_rohit31-Dec-14 11:03 
GeneralMy vote of 1 Pin
waliullah.wa7-Dec-14 23:01
waliullah.wa7-Dec-14 23:01 
GeneralRe: My vote of 1 Pin
maq_rohit31-Dec-14 11:03
professionalmaq_rohit31-Dec-14 11:03 
QuestionService got down - http://j.maxmind.com/app/geoip.js Pin
Uday Gaikwad18-Nov-14 21:42
Uday Gaikwad18-Nov-14 21:42 
AnswerRe: Service got down - http://j.maxmind.com/app/geoip.js Pin
maq_rohit19-Nov-14 7:09
professionalmaq_rohit19-Nov-14 7:09 
GeneralRe: Service got down - http://j.maxmind.com/app/geoip.js Pin
Uday Gaikwad19-Nov-14 22:14
Uday Gaikwad19-Nov-14 22:14 
QuestionGreat Pin
AnandMeena9-Aug-13 23:56
AnandMeena9-Aug-13 23:56 
AnswerRe: Great Pin
maq_rohit21-Aug-13 12:58
professionalmaq_rohit21-Aug-13 12:58 
Suggestionnot good Pin
Member 984651419-Feb-13 20:00
Member 984651419-Feb-13 20:00 
to include this code in my website i need source code right so can give some other suggestion rohit?????????if source code is not available then speed of my website will not effective Cry | :((
GeneralRe: not good Pin
albert arul prakash15-Apr-13 20:46
albert arul prakash15-Apr-13 20:46 
Questioncomment Pin
Member 984651418-Feb-13 23:59
Member 984651418-Feb-13 23:59 
GeneralMy vote of 5 Pin
Member 984651418-Feb-13 23:58
Member 984651418-Feb-13 23:58 
QuestionUnbelievable Pin
Biswajit Giri4-Sep-12 5:08
Biswajit Giri4-Sep-12 5:08 
GeneralMy vote of 5 Pin
jrickegard17-Aug-12 1:02
professionaljrickegard17-Aug-12 1:02 
GeneralRe: My vote of 5 Pin
maq_rohit17-Aug-12 19:52
professionalmaq_rohit17-Aug-12 19:52 
GeneralMy vote of 5 Pin
tanweer1-Jul-12 20:07
tanweer1-Jul-12 20:07 
GeneralRe: My vote of 5 Pin
maq_rohit17-Aug-12 19:53
professionalmaq_rohit17-Aug-12 19:53 
Questionhttp://j.maxmind.com/app/geoip.js Pin
idlez29-Mar-12 16:02
idlez29-Mar-12 16:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.