Class GeoLocation
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.
Status Handling
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());
}
Status Values
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 Availability by Database Type
| Field | DB1 | DB3 | DB5 | DB9 | DB11 |
|---|---|---|---|---|---|
| Country code/name | ✓ | ✓ | ✓ | ✓ | ✓ |
| Region | ✓ | ✓ | ✓ | ✓ | |
| City | ✓ | ✓ | ✓ | ✓ | |
| Latitude/Longitude | ✓ | ✓ | ✓ | ||
| ZIP Code | ✓ | ✓ | |||
| Timezone | ✓ |
Location Description
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"
}
Thread Safety
This class is immutable and therefore inherently thread-safe. Instances can be safely shared between threads without synchronization.
Creating Instances
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();
- Author:
- Christopher Mindus
- See Also:
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creatingGeoLocationinstances.static enumEnumeration of possible lookup result statuses.Method Summary
Modifier and TypeMethodDescriptionstatic GeoLocation.Builderbuilder()Creates a new builder for constructing GeoLocation instances.booleanCompares this GeoLocation with another object for equality.static GeoLocationerror(String ip, GeoLocation.Status status) Creates an error result with the specified status.getCity()Returns the city name.Returns the ISO 3166-1 alpha-2 country code.Returns the full country name.getIp()Returns the IP address that was looked up.doubleReturns the latitude in decimal degrees.Returns a human-readable description of the location.getLocationDescription(boolean includeCoordinates) Returns a human-readable description of the location.doubleReturns the longitude in decimal degrees.Returns the region or state name.Returns the status of the lookup operation.Returns the timezone in IANA tz database format.Returns the ZIP or postal code.inthashCode()Returns a hash code for this GeoLocation.booleanisIPv6()Returns whether the looked-up IP address was an IPv6 address.booleanisOk()Returns whether the lookup was successful.toString()Returns a string representation of this geolocation result.
Method Details
getIp
Returns the IP address that was looked up.- Returns:
- The IP address string as provided to the lookup method
getStatus
Returns the status of the lookup operation.- Returns:
- The status enum value
- See Also:
isOk
public boolean isOk()Returns whether the lookup was successful.This is equivalent to checking if
getStatus() == Status.OK.- Returns:
trueif the lookup succeeded and location data is available
isIPv6
public boolean isIPv6()Returns whether the looked-up IP address was an IPv6 address.- Returns:
trueif the IP was IPv6,falseif IPv4
getCountryCode
Returns the ISO 3166-1 alpha-2 country code.Available in all database types (DB1+).
- Returns:
- Two-letter country code (e.g., "US", "DE", "JP"), or
null/"-" if unavailable
getCountryName
Returns the full country name.Available in all database types (DB1+).
- Returns:
- Country name (e.g., "United States of America"), or
null/"-" if unavailable
getRegion
Returns the region or state name.Requires DB3 or higher. Returns
nullor "-" if unavailable.- Returns:
- Region/state name (e.g., "California"), or
null/"-" if unavailable
getCity
Returns the city name.Requires DB3 or higher. Returns
nullor "-" if unavailable.- Returns:
- City name (e.g., "Mountain View"), or
null/"-" if unavailable
getLatitude
public double getLatitude()Returns the latitude in decimal degrees.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.
- Returns:
- Latitude in range -90.0 (South Pole) to 90.0 (North Pole)
getLongitude
public double getLongitude()Returns the longitude in decimal degrees.Requires DB5 or higher. Returns 0.0 if unavailable.
- Returns:
- Longitude in range -180.0 (West) to 180.0 (East)
getZipCode
Returns the ZIP or postal code.Requires DB9 or higher. Returns
nullor "-" if unavailable.Note: Format varies by country (e.g., "94043" for US, "80331" for Germany).
- Returns:
- ZIP/postal code string, or
null/"-" if unavailable
getTimeZone
Returns the timezone in IANA tz database format.Requires DB11. Returns
nullor "-" 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); }- Returns:
- Timezone string (e.g., "America/Los_Angeles", "Europe/Berlin"), or
null/"-" - See Also:
getLocationDescription
Returns a human-readable description of the location.Equivalent to
getLocationDescription(false).- Returns:
- A comma-separated string of location fields, or empty string if no data
- See Also:
getLocationDescription
Returns a human-readable description of the location.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):
- City
- Region/State
- Country name (or country code if name is not available)
- ZIP/Postal code
- Timezone
- Coordinates (if includeCoordinates is true and both lat/long are non-zero)
Example outputs:
- "Mountain View, California, United States of America, 94043, America/Los_Angeles"
- "Munich, Bavaria, Germany"
- "Tokyo, Japan, Asia/Tokyo, 35.6895°N, 139.6917°E" (with coordinates)
- "US" (if only country code is available)
- "" (empty string if lookup failed or no data)
- Parameters:
includeCoordinates-trueto include latitude/longitude if available- Returns:
- A comma-separated string of location fields, or empty string if no data
toString
equals
hashCode
error
Creates an error result with the specified status.- Parameters:
ip- The IP address that was looked up (may be null)status- The error status- Returns:
- A GeoLocation with the error status and no location data
builder
Creates a new builder for constructing GeoLocation instances.- Returns:
- A new Builder instance