public final class GeoLocation extends Object
This class encapsulates all geographic information that can be determined from
an IP address using the IP2Location database. The availability of each field depends
on which Database variant is being used.
Always check isOk() before accessing location data:
GeoLocation loc = service.lookup("8.8.8.8");
if (loc.isOk()) {
System.out.println("Country: " + loc.getCountryCode());
System.out.println("City: " + loc.getCity());
System.out.println("Location: " + loc.getLocationDescription());
} else {
System.err.println("Lookup failed: " + loc.getStatus());
}
The GeoLocation.Status enum provides type-safe status codes:
| Status | Meaning |
|---|---|
GeoLocation.Status.OK | Lookup successful, all fields populated |
GeoLocation.Status.EMPTY_IP | IP address was null or empty |
GeoLocation.Status.INVALID_IP | IP address format was invalid |
GeoLocation.Status.NOT_FOUND | IP not found in database |
GeoLocation.Status.DB_NOT_LOADED | No database loaded |
GeoLocation.Status.IPV6_NOT_SUPPORTED | IPv6 lookup but no IPv6 DB |
GeoLocation.Status.DB_ERROR | I/O error during lookup |
GeoLocation.Status.SERVICE_DISABLED | Service is disabled |
GeoLocation.Status.SERVICE_DISPOSED | Service has been disposed |
GeoLocation.Status.SERVICE_SHUTDOWN | Manager has been shut down |
GeoLocation.Status.SERVICE_NOT_CONFIGURED | Manager not configured |
GeoLocation.Status.SERVICE_RECONFIGURING | Service is reconfiguring |
| Field | DB1 | DB3 | DB5 | DB9 | DB11 |
|---|---|---|---|---|---|
| Country code/name | ✓ | ✓ | ✓ | ✓ | ✓ |
| Region | ✓ | ✓ | ✓ | ✓ | |
| City | ✓ | ✓ | ✓ | ✓ | |
| Latitude/Longitude | ✓ | ✓ | ✓ | ||
| ZIP Code | ✓ | ✓ | |||
| Timezone | ✓ |
Use getLocationDescription() or getLocationDescription(boolean)
to get a human-readable string of all defined location fields:
GeoLocation loc = service.lookup("8.8.8.8");
if (loc.isOk()) {
// Without coordinates
String desc = loc.getLocationDescription();
// "Mountain View, California, United States of America, 94043, America/Los_Angeles"
// With coordinates
String descWithCoords = loc.getLocationDescription(true);
// "Mountain View, California, United States of America, 94043, America/Los_Angeles, 37.4056°N, 122.0775°W"
}
This class is immutable and therefore inherently thread-safe. Instances can be safely shared between threads without synchronization.
Use the GeoLocation.Builder for programmatic creation:
GeoLocation loc = GeoLocation.builder()
.ip("8.8.8.8")
.status(GeoLocation.Status.OK)
.countryCode("US")
.countryName("United States of America")
.city("Mountain View")
.latitude(37.4056)
.longitude(-122.0775)
.build();
IP2LocationService.lookup(String),
IP2LocationManager.lookup(String),
Database,
GeoLocation.Status| Modifier and Type | Class and Description |
|---|---|
static class |
GeoLocation.Builder
Builder for creating
GeoLocation instances. |
static class |
GeoLocation.Status
Enumeration of possible lookup result statuses.
|
| Modifier and Type | Method and Description |
|---|---|
static GeoLocation.Builder |
builder()
Creates a new builder for constructing GeoLocation instances.
|
boolean |
equals(Object o)
Compares this GeoLocation with another object for equality.
|
static GeoLocation |
error(String ip,
GeoLocation.Status status)
Creates an error result with the specified status.
|
String |
getCity()
Returns the city name.
|
String |
getCountryCode()
Returns the ISO 3166-1 alpha-2 country code.
|
String |
getCountryName()
Returns the full country name.
|
String |
getIp()
Returns the IP address that was looked up.
|
double |
getLatitude()
Returns the latitude in decimal degrees.
|
String |
getLocationDescription()
Returns a human-readable description of the location.
|
String |
getLocationDescription(boolean includeCoordinates)
Returns a human-readable description of the location.
|
double |
getLongitude()
Returns the longitude in decimal degrees.
|
String |
getRegion()
Returns the region or state name.
|
GeoLocation.Status |
getStatus()
Returns the status of the lookup operation.
|
String |
getTimeZone()
Returns the timezone in IANA tz database format.
|
String |
getZipCode()
Returns the ZIP or postal code.
|
int |
hashCode()
Returns a hash code for this GeoLocation.
|
boolean |
isIPv6()
Returns whether the looked-up IP address was an IPv6 address.
|
boolean |
isOk()
Returns whether the lookup was successful.
|
String |
toString()
Returns a string representation of this geolocation result.
|
public String getIp()
public GeoLocation.Status getStatus()
isOk(),
GeoLocation.Statuspublic boolean isOk()
This is equivalent to checking if getStatus() == Status.OK.
true if the lookup succeeded and location data is availablepublic boolean isIPv6()
true if the IP was IPv6, false if IPv4public String getCountryCode()
Available in all database types (DB1+).
null/"-" if unavailablepublic String getCountryName()
Available in all database types (DB1+).
null/"-" if unavailablepublic String getRegion()
Requires DB3 or higher. Returns null or "-" if unavailable.
null/"-" if
unavailablepublic String getCity()
Requires DB3 or higher. Returns null or "-" if unavailable.
null/"-" if
unavailablepublic double getLatitude()
Requires DB5 or higher. Returns 0.0 if unavailable.
Note: For reserved/private IP ranges, coordinates may be 0.0 or point to a country's geographic center.
public double getLongitude()
Requires DB5 or higher. Returns 0.0 if unavailable.
public String getZipCode()
Requires DB9 or higher. Returns null or "-" if unavailable.
Note: Format varies by country (e.g., "94043" for US, "80331" for Germany).
null/"-" if unavailablepublic String getTimeZone()
Requires DB11. Returns null or "-" if unavailable.
The returned value can be used with ZoneId.of(String):
if (loc.getTimeZone() != null && !"-".equals(loc.getTimeZone())) {
ZoneId zone = ZoneId.of(loc.getTimeZone());
ZonedDateTime localTime = ZonedDateTime.now(zone);
}
null/"-"ZoneId.of(String)public String getLocationDescription()
Equivalent to getLocationDescription(false).
getLocationDescription(boolean)public String getLocationDescription(boolean includeCoordinates)
This method returns only the fields that have defined values (not null, not empty, not "-"), concatenated in order of importance with ", " as separator.
Field order (most important first):
includeCoordinates - true to include latitude/longitude if availablepublic String toString()
For successful lookups, includes IP, city, region, and country. For failed lookups, includes IP and status.
public boolean equals(Object o)
Two GeoLocation objects are equal if all their fields are equal.
public int hashCode()
public static GeoLocation error(String ip, GeoLocation.Status status)
ip - The IP address that was looked up (may be null)status - The error statuspublic static GeoLocation.Builder builder()
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.