How to get city name based on IP address in java?

17,775

Solution 1

are you using jsp or what ? for city name over client side you can use google's API which will return you the geographical details of the user mapped with the IP. As google's documentation say you can get the following

When populated, the google.loader.ClientLocation object is populated with the following metro-level granularity properties:

* ClientLocation.latitude — supplies the low resolution latitude associated with the client's IP address
* ClientLocation.longitude — supplies the low resolution longitude associated with the client's IP address
* ClientLocation.address.city — supplies the name of the city associated with the client's IP address
* ClientLocation.address.country — supplies the name of the country associated with the client's IP address
* ClientLocation.address.country_code — supplies the name of the ISO 3166-1 country code associated with the client's IP address
* ClientLocation.address.region — supplies the country specific region name associated with the client's IP address

for further reference see the complete documentation at http://code.google.com/apis/ajax/documentation/

Solution 2

You have to use a service such as hostip.info that maps an IP to its physical location, and offer an API to developers to simplify the task.

There are many other services, paid and for free, that you can find at Google.

Solution 3

If an online API as the others have suggested won't suit your application, I have previously successfully used MaxMind GeoLite City, which comes with a Java API (among many others). Just download the database files, point your API code at them, and off you go. Simple and straightforward.

Share:
17,775
Abhishek Dhote
Author by

Abhishek Dhote

A Hewlett Packard long-timer, innovator and a products guy with keen interest in social entrepreneurship. Built and architected enterprise products at HP. Conceptualization and developed products, launched social startup, hired people and working on growing team and revenue to profitability.

Updated on July 14, 2022

Comments

  • Abhishek Dhote
    Abhishek Dhote almost 2 years

    Is it possible to know the city name based on IP address in java?

  • Abhishek Dhote
    Abhishek Dhote over 14 years
    Yes I am using JSP infact JSF, thanks for your quick responce, if you can provide me code snipet, then that will really help!
  • Pedantic
    Pedantic over 12 years
    does this service is paid, if yes will you please suggest any free service. just-flick.com