1. Brief description
In Geographic Information Systems (GIS) and navigation applications, accurate geographic calculations are the basis. Geodesy is a popular Java library used to handle geographic location, distance, direction and other related calculations. It is based on the WGS84 coordinate system and provides a wealth of tools for a variety of geographic computing needs.
This blog will introduce the core features of Geodesy and provide detailed practical examples to help developers get started quickly.
2. Core functions
- Geographic distance calculation
Calculate the shortest path distance (large circle distance) between two points. - Azimuth calculation
Calculate the direction from one point to another. - Movement of points
Calculate another point from one point based on the azimuth and distance. - Regional boundaries
Calculate the boundary of the area centered at a certain point and the specified distance.
Add the following dependencies to your Maven project:
<dependency> <groupId></groupId> <artifactId>geodesy</artifactId> <version>1.1.3</version> </dependency>
3. Application examples
3.1 Calculate the distance between two points
Calculate the distance between two points on the earth's surface.
import .*; public class GeodesyExample { public static void main(String[] args) { // Define two geographical locations (longitude, latitude) GlobalCoordinates pointA = new GlobalCoordinates(34.052235, -118.243683); // Los Angeles GlobalCoordinates pointB = new GlobalCoordinates(40.712776, -74.005974); // New York // Use WGS84 ellipsoid GeodeticCalculator calculator = new GeodeticCalculator(); Ellipsoid reference = Ellipsoid.WGS84; // Calculate distance (meters) GeodeticCurve curve = (reference, pointA, pointB); double distance = (); ("Distance from Los Angeles to New York: " + distance / 1000 + " kilometer"); } }
3.2 Calculate the azimuth angle
Calculate the direction angle from one point to another.
public class BearingExample { public static void main(String[] args) { GlobalCoordinates pointA = new GlobalCoordinates(34.052235, -118.243683); // Los Angeles GlobalCoordinates pointB = new GlobalCoordinates(40.712776, -74.005974); // New York GeodeticCalculator calculator = new GeodeticCalculator(); Ellipsoid reference = Ellipsoid.WGS84; // Calculate the azimuth angle GeodeticCurve curve = (reference, pointA, pointB); double azimuth = (); ("Los Angeles to New York's azimuth: " + azimuth + " Spend"); } }
3.3 Calculate new geographical locations based on distance and direction
Starting from one point, calculate the latitude and longitude of the target point based on the direction and distance.
public class DestinationExample { public static void main(String[] args) { GlobalCoordinates startPoint = new GlobalCoordinates(34.052235, -118.243683); // Los Angeles // Distance (meters) and direction (angle) double distance = 100000; // 100 km double azimuth = 45; // Northeast direction GeodeticCalculator calculator = new GeodeticCalculator(); Ellipsoid reference = Ellipsoid.WGS84; GlobalCoordinates destination = ( reference, startPoint, azimuth, distance); ("Latitude and longitude of the new location: "); ("Latitude: " + ()); ("Longitude: " + ()); } }
3.4 Calculate the boundary of the point
Gets the boundary of a circular area centered at a point.
public class BoundaryExample { public static void main(String[] args) { GlobalCoordinates center = new GlobalCoordinates(34.052235, -118.243683); // Los Angeles // Distance range (meters) double radius = 50000; // 50 km GeodeticCalculator calculator = new GeodeticCalculator(); Ellipsoid reference = Ellipsoid.WGS84; // Calculate the boundary points in 4 directions double[] azimuths = {0, 90, 180, 270}; // North, East, South, West for (double azimuth : azimuths) { GlobalCoordinates boundaryPoint = ( reference, center, azimuth, radius); ("Azimuth" + azimuth + "The boundary point:"); ("Latitude: " + ()); ("Longitude: " + ()); } } }
4. Use scenarios
- Logistics and Navigation
Calculate the delivery path and delivery area. - Geo-fencing
Define a specific area in the application and determine whether the user is within range. - Distance sorting
Sort by distance between user and target location in the service. - Real-time tracking
Used for real-time GPS-based monitoring systems.
5. Conclusion
Geodesy is a powerful tool for handling geographic computing. Its API is simple and easy to use, making it ideal for GIS and navigation applications that require high-precision computing. Through the example code in this article, I believe everyone can easily get started and flexibly apply it to actual scenarios.
This is the end of this article about Java's technical guide to using Geodesy for geographic computing. For more related Java Geodesy geographic computing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!