When you're building geospatial applications, it's surprisingly easy for users to accidentally create duplicate or very close records. Imagine trying to add a location (like a traffic signal or utility pole) at a GPS point that’s just 30 feet away from an existing one. Should the system allow that? Should the user be warned?
In this post, I’ll show how I implemented a proximity detection system using the Haversine formula in Java to solve this problem cleanly and effectively.
🎯 The Requirement
My goal was to prevent duplicate entries by:
- Calculating the distance between the new location and existing ones
- Alerting the user if any location was within X feet (say, 100 ft)
- Letting them decide whether to continue
So instead of relying on exact latitude/longitude matches, we used distance-based detection.
🌍 Why Haversine?
The Earth is round, not flat — and the Haversine formula accounts for that. It calculates the shortest distance over the Earth's surface between two latitude/longitude points, returning a value in meters (or feet, in our case).
💻 Java Code: Checking Nearby Locations
Here’s how I implemented it in Java:
Step 1: Find All Locations Within a Given Distance
public static List<Location> findNearbyLocations(List<Location> locations, double newLat, double newLon, double maxDistanceInFeet) {
List<Location> nearby = new ArrayList<>();
for (Location loc : locations) {
double distance = calculateDistanceInFeet(newLat, newLon, loc.getLatitude(), loc.getLongitude());
loc.setDistanceInFeet(distance);
if (distance <= maxDistanceInFeet) {
nearby.add(loc);
}
}
return nearby;
}
Step 2: Calculate Distance Using Haversine Formula
public static double calculateDistanceInFeet(double lat1, double lon1, double lat2, double lon2) {
double earthRadiusMeters = 6371000.0;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
double angularDistance = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distanceMeters = earthRadiusMeters * angularDistance;
return Math.ceil(distanceMeters * 3.28084); // Convert to feet
}
🧪 Example Usage
List<Location> existingLocations = fetchFromDatabase();
double newLat = 40.4406;
double newLon = -79.9959;
List<Location> nearby = findNearbyLocations(existingLocations, newLat, newLon, 100);
if (!nearby.isEmpty()) {
System.out.println("⚠️ A location already exists within 100 feet:");
for (Location loc : nearby) {
System.out.println(" - ID: " + loc.getId() + " | Distance: " + loc.getDistanceInFeet() + " feet");
}
}
🔄 Real-World Applications
This technique can help with:
Deduplication in asset management systems
Avoiding overlapping POIs in mapping tools
Notifying users of nearby infrastructure
You could extend this further by:
Visualizing nearby matches on a map
Using a UI modal with “Continue Anyway” option
Applying clustering or spatial indexing for large datasets
🧠 Final Thoughts
The Haversine formula gave us a lightweight and accurate way to detect duplicate or nearby location entries. It’s especially useful when latitude and longitude are just a few decimal points apart but still physically very close.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.