Click here to Skip to main content
15,122,845 members
Articles / Programming Languages / Javascript
Tip/Trick
Posted 5 May 2011

Tagged as

Stats

200K views
12 bookmarked

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
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)

Share

About the Author

maq_rohit
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 20:49
MemberMember 1206274616-Oct-15 20:49 
GeneralRe: for using a api Pin
maq_rohit2-Nov-15 10:22
professionalmaq_rohit2-Nov-15 10:22 
use this
//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js

asthanarht

QuestionReferenceError: geoip_country_name is not defined Pin
Member 1163244922-Apr-15 0:53
MemberMember 1163244922-Apr-15 0:53 
AnswerRe: ReferenceError: geoip_country_name is not defined Pin
maq_rohit5-May-15 14:50
professionalmaq_rohit5-May-15 14:50 
GeneralMy vote of 3 Pin
Anand_Mpec28-Dec-14 20:13
MemberAnand_Mpec28-Dec-14 20:13 
GeneralRe: My vote of 3 Pin
maq_rohit5-May-15 14:55
professionalmaq_rohit5-May-15 14:55 
GeneralMy vote of 1 Pin
Dnyaneshwar@Pune22-Dec-14 23:43
MemberDnyaneshwar@Pune22-Dec-14 23:43 
GeneralRe: My vote of 1 Pin
maq_rohit31-Dec-14 12:03
professionalmaq_rohit31-Dec-14 12:03 
GeneralMy vote of 1 Pin
waliullah.wa8-Dec-14 0:01
Memberwaliullah.wa8-Dec-14 0:01 
GeneralRe: My vote of 1 Pin
maq_rohit31-Dec-14 12:03
professionalmaq_rohit31-Dec-14 12:03 
QuestionService got down - http://j.maxmind.com/app/geoip.js Pin
Uday Gaikwad18-Nov-14 22:42
MemberUday Gaikwad18-Nov-14 22:42 
AnswerRe: Service got down - http://j.maxmind.com/app/geoip.js Pin
maq_rohit19-Nov-14 8:09
professionalmaq_rohit19-Nov-14 8:09 
GeneralRe: Service got down - http://j.maxmind.com/app/geoip.js Pin
Uday Gaikwad19-Nov-14 23:14
MemberUday Gaikwad19-Nov-14 23:14 
QuestionGreat Pin
AnandMeena10-Aug-13 0:56
MemberAnandMeena10-Aug-13 0:56 
AnswerRe: Great Pin
maq_rohit21-Aug-13 13:58
professionalmaq_rohit21-Aug-13 13:58 
Suggestionnot good Pin
Member 984651419-Feb-13 21:00
MemberMember 984651419-Feb-13 21:00 
GeneralRe: not good Pin
albert arul prakash15-Apr-13 21:46
Memberalbert arul prakash15-Apr-13 21:46 
Questioncomment Pin
Member 984651419-Feb-13 0:59
MemberMember 984651419-Feb-13 0:59 
GeneralMy vote of 5 Pin
Member 984651419-Feb-13 0:58
MemberMember 984651419-Feb-13 0:58 
QuestionUnbelievable Pin
Biswajit Giri4-Sep-12 6:08
MemberBiswajit Giri4-Sep-12 6:08 
GeneralMy vote of 5 Pin
jrickegard17-Aug-12 2:02
professionaljrickegard17-Aug-12 2:02 
GeneralRe: My vote of 5 Pin
maq_rohit17-Aug-12 20:52
professionalmaq_rohit17-Aug-12 20:52 
GeneralMy vote of 5 Pin
tanweer1-Jul-12 21:07
Membertanweer1-Jul-12 21:07 
GeneralRe: My vote of 5 Pin
maq_rohit17-Aug-12 20:53
professionalmaq_rohit17-Aug-12 20:53 
Questionhttp://j.maxmind.com/app/geoip.js Pin
idlez29-Mar-12 17:02
Memberidlez29-Mar-12 17: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.