Const
The first latitude in degrees
The first longitude in degrees
The second latitude in degrees
The second longitude in degrees
The bearing in degrees (0-360) from north
// Calculate bearing from New York to London
const bearing = LocationUtils.calculateBearing(40.7128, -74.0060, 51.5074, -0.1278);
console.log(`Bearing: ${bearing.toFixed(1)}°`); // "Bearing: 77.5°"
// Calculate bearing between two points in the same city
const localBearing = LocationUtils.calculateBearing(40.7128, -74.0060, 40.7589, -73.9851);
console.log(`Local bearing: ${localBearing.toFixed(1)}°`); // "Local bearing: 45.2°"
Calculates a new coordinate point based on a starting point, distance, and bearing using the Haversine formula.
The starting latitude in degrees
The starting longitude in degrees
The distance to travel in kilometers
The bearing (direction) in degrees (0-360)
An object containing the new latitude and longitude coordinates
// Calculate a point 100km northeast of New York
const destination = LocationUtils.calculateDestination(40.7128, -74.0060, 100, 45);
console.log(`Destination: ${destination.latitude.toFixed(4)}, ${destination.longitude.toFixed(4)}`);
// "Destination: 41.4000, -73.1000"
// Calculate a point 50km south of a location
const southPoint = LocationUtils.calculateDestination(40.7128, -74.0060, 50, 180);
console.log(`South point: ${southPoint.latitude.toFixed(4)}, ${southPoint.longitude.toFixed(4)}`);
Calculates the great-circle distance between two geographic coordinates using the Haversine formula.
The first latitude in degrees
The first longitude in degrees
The second latitude in degrees
The second longitude in degrees
The distance in kilometers
// Calculate distance between New York and London
const distance = LocationUtils.calculateDistance(40.7128, -74.0060, 51.5074, -0.1278);
console.log(`Distance: ${distance.toFixed(1)} km`); // "Distance: 5570.2 km"
// Calculate distance between two points in the same city
const localDistance = LocationUtils.calculateDistance(40.7128, -74.0060, 40.7589, -73.9851);
console.log(`Local distance: ${localDistance.toFixed(2)} km`); // "Local distance: 8.45 km"
Stops watching the user's position that was previously started with watchPosition.
The watch ID returned by watchPosition
If geolocation is not supported by the browser
// Start watching position
const watchId = LocationUtils.watchPosition(position => {
console.log('Position updated:', position.coords);
});
// Later, stop watching
LocationUtils.clearWatch(watchId);
console.log('Position watching stopped');
Formats geographic coordinates as a string in either decimal degrees (DD) or degrees, minutes, seconds (DMS) format.
The latitude in degrees
The longitude in degrees
The format to use: 'DD' for decimal degrees or 'DMS' for degrees, minutes, seconds
A formatted coordinate string
// Decimal degrees format
const dd = LocationUtils.formatCoordinate(40.7128, -74.0060, 'DD');
console.log(dd); // "40.712800, -74.006000"
// Degrees, minutes, seconds format
const dms = LocationUtils.formatCoordinate(40.7128, -74.0060, 'DMS');
console.log(dms); // "40° 42' 46.08" N, 74° 0' 21.60" W"
// Southern hemisphere example
const sydney = LocationUtils.formatCoordinate(-33.8688, 151.2093, 'DMS');
console.log(sydney); // "33° 52' 7.68" S, 151° 12' 33.48" E"
Gets the user's current position using the browser's Geolocation API.
Geolocation options for accuracy, timeout, and maximum age
A Promise that resolves with the GeolocationPosition object
If geolocation is not supported by the browser
try {
const position = await LocationUtils.getCurrentPosition({
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
console.log(`Latitude: ${position.coords.latitude}`);
console.log(`Longitude: ${position.coords.longitude}`);
console.log(`Accuracy: ${position.coords.accuracy} meters`);
} catch (error) {
console.error('Error getting position:', error);
}
Calculates the center point of a geographic bounding box.
The bounding box with north, south, east, and west coordinates
An object containing the latitude and longitude of the center point
const bounds = {
north: 41.0,
south: 40.0,
east: -73.0,
west: -74.0
};
const center = LocationUtils.getBoundsCenter(bounds);
console.log(`Center: ${center.latitude.toFixed(4)}, ${center.longitude.toFixed(4)}`);
// "Center: 40.5000, -73.5000"
Calculates a bounding box for a given radius around a center point.
The center latitude in degrees
The center longitude in degrees
The radius in kilometers
A bounding box with north, south, east, and west coordinates
// Get bounding box for 10km radius around New York
const bounds = LocationUtils.getBoundsForRadius(40.7128, -74.0060, 10);
console.log('Bounding box:', bounds);
// {
// north: 40.8028,
// south: 40.6228,
// east: -73.9060,
// west: -74.1060
// }
// Check if a point is within this radius
const isInside = LocationUtils.isWithinBounds(40.7589, -73.9851, bounds);
console.log(`Point is within 10km: ${isInside}`); // true
Checks if a coordinate point is within a geographic bounding box.
The latitude to check in degrees
The longitude to check in degrees
The bounding box with north, south, east, and west coordinates
True if the coordinate is within the bounds, false otherwise
const bounds = {
north: 41.0,
south: 40.0,
east: -73.0,
west: -75.0
};
// Check if New York is within bounds
const isInside = LocationUtils.isWithinBounds(40.7128, -74.0060, bounds);
console.log(`New York is within bounds: ${isInside}`); // true
// Check if London is within bounds
const isLondonInside = LocationUtils.isWithinBounds(51.5074, -0.1278, bounds);
console.log(`London is within bounds: ${isLondonInside}`); // false
Parses a coordinate string in decimal degrees format into latitude and longitude values.
The coordinate string in "latitude, longitude" format
An object containing the parsed latitude and longitude
If the coordinate string is invalid or cannot be parsed
// Parse a coordinate string
const coord = LocationUtils.parseCoordinate("40.7128, -74.0060");
console.log(`Latitude: ${coord.latitude}, Longitude: ${coord.longitude}`);
// "Latitude: 40.7128, Longitude: -74.0060"
try {
// Invalid format
const invalid = LocationUtils.parseCoordinate("invalid");
} catch (error) {
console.error('Error:', error.message); // "Invalid coordinate format"
}
Converts an angle from radians to degrees.
The angle in radians
The angle in degrees
const degrees = LocationUtils.toDeg(Math.PI);
console.log(`${degrees}°`); // "180°"
const halfCircle = LocationUtils.toDeg(Math.PI / 2);
console.log(`${halfCircle}°`); // "90°"
Converts an angle from degrees to radians.
The angle in degrees
The angle in radians
const radians = LocationUtils.toRad(180);
console.log(radians); // 3.141592653589793 (Math.PI)
const halfCircle = LocationUtils.toRad(90);
console.log(halfCircle); // 1.5707963267948966 (Math.PI / 2)
Starts watching the user's position using the browser's Geolocation API.
The callback function to be called with position updates
Geolocation options for accuracy, timeout, and maximum age
A watch ID that can be used to stop watching with clearWatch
If geolocation is not supported by the browser
// Start watching position with high accuracy
const watchId = LocationUtils.watchPosition(
position => {
console.log('Position updated:');
console.log(`Latitude: ${position.coords.latitude}`);
console.log(`Longitude: ${position.coords.longitude}`);
console.log(`Accuracy: ${position.coords.accuracy} meters`);
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
);
// Later, stop watching
LocationUtils.clearWatch(watchId);
Calculates the bearing (direction) between two geographic coordinates using the Haversine formula.