Innovative GPS-Based Angular Calculations in JavaScript
Written on
To determine the direction, distance, and altitude between two GPS locations using JavaScript, one can employ a specialized algorithm. This method is particularly helpful for tasks such as positioning a drone or aiming a satellite dish. Below, I'll guide you through the necessary calculations and provide sample code.
Input Requirements
For the calculations, six numbers are required—three for each location. The starting point is termed Point A, while the destination is Point B. For both locations, the following information is essential:
- Latitude: The degree of north or south from the equator, where positive denotes north and negative south.
- Longitude: The degree of east (positive) or west (negative) from the prime meridian located in Greenwich, England.
- Elevation: The height in meters above (positive) or below (negative) mean sea level.
Geostationary Satellites
This algorithm is also applicable when the target is a geostationary satellite. Such satellites orbit directly above the equator (latitude = 0 degrees) at an elevation of 35,786 kilometers. The only missing parameter is the satellite’s longitude, which can be easily entered in my online calculator designed for this purpose. This feature has proven useful for individuals aiming satellite dishes.
Output Results
Once the coordinates are provided, the algorithm computes the following:
- Azimuth: The compass direction from Point A to Point B, represented as an angle between 0 and 360 degrees, where 0 indicates north, 90 east, 180 south, and 270 west.
- Altitude: The angle above (positive) or below (negative) the horizon of Point B from the perspective of an observer at Point A.
- Distance: The straight-line distance (not accounting for the Earth's curvature) from Point A to Point B in kilometers.
Operational Mechanics
Initially, the algorithm converts both locations into Cartesian coordinates (x, y, z). This process is complex due to the Earth's shape; it is not a perfect sphere but rather an oblate spheroid. The distance from pole to pole measures 6357 km, while the equatorial diameter reaches 6378 km.
In the diagram, point X represents a location on Earth, with N indicating the North Pole, S the South Pole, and C the Earth's center. The equator is depicted by the line through C. Observers at point X can see their local horizon represented by the dotted line, with another line indicating the angle of geodetic latitude, which GPS measures.
To calculate Cartesian coordinates for point X relative to the center C, the algorithm requires geocentric latitude. This angle, indicated by ? in the diagram, differs from geodetic latitude except at the equator and poles.
The algorithm utilizes the function GeocentricLatitude to achieve this conversion. Similarly, the distance from the Earth's center to any surface point is computed by EarthRadiusInMeters, which considers geodetic latitude.
Interestingly, geocentric and geodetic latitudes align at the equator (0 degrees) and poles (±90 degrees) but differ elsewhere.
The calculator employs the LocationToPoint function to ascertain Cartesian coordinates from geodetic latitude, longitude, and elevation. This function also computes the normal vector for accurate elevation adjustment.
To finalize the calculations, the algorithm rotates the coordinate system to position the observation point (Point A) at a pretend equator and prime meridian. This facilitates the computation of azimuth and altitude angles.
- The x-axis extends upward.
- The y-axis points east.
- The z-axis directs north.
The rotation is executed via the RotateGlobe function.
With the rotated coordinate system in place, a line can be drawn from Point A to Point B through three-dimensional space, typically passing through the Earth. The algorithm then applies trigonometric functions and vector dot products to derive the azimuth and altitude angles and uses the Pythagorean theorem to measure the distance.
All code is accessible through the links provided below.
Resources
- Online Calculator: Perform calculations directly in your browser.
- GitHub Repository: Access the complete HTML and JavaScript code for the calculator.
Sample Applications
Here are three intriguing projects that utilized my JavaScript code (with permission for use of names and images).
#### Example #1: ISS Pointer
Jan Steinberg and his team at the Bonn-Rhein-Sieg University developed a device that continuously points to the International Space Station (ISS). By calculating the ISS's orbit and the device's location on the ground, they compute the necessary azimuth and altitude angles, which are then adjusted using stepper motors.
#### Example #2: The Pterrascope
Rick Hemmings from Mendocino, California, utilized the Altitude/Azimuth calculator to create a sculpture that points toward various global cities. This innovative design aims directly at those cities at the correct angles, piercing through the Earth.
#### Example #3: Moonrise Photography
Ole Jørgen Nordhagen from Sola, Norway, made use of the Azimuth/Distance calculator to determine optimal times and locations for photographing the Moon and Sun behind chosen terrestrial features.
Additional Applications
- Drones equipped with GPS can automate direction and range calculations, assisting users in locating their drones post-landing.
- Sailboat racers leverage this tool to strategize routes through required waypoints.
- Microwave technicians utilize the calculator for precise alignment of microwave relays.
- Satellite dish installers find the tool beneficial for aiming dishes toward specific satellites, especially with the added geostationary satellite checkbox.
Originally, I developed this tool to experiment with magnetic loop antennas for AM receivers, which are highly sensitive to broadcast tower directions. By correlating GPS coordinates, I could verify signal reception from expected towers.
I welcome comments on how you have implemented this code in your projects!