Variable LocationUtilsConst

LocationUtils: {
    calculateBearing(lat1, lon1, lat2, lon2): number;
    calculateDestination(lat, lon, distance, bearing): {
        latitude: number;
        longitude: number;
    };
    calculateDistance(lat1, lon1, lat2, lon2): number;
    clearWatch(watchId): void;
    formatCoordinate(lat, lon, format?): string;
    getCurrentPosition(options?): Promise<GeolocationPosition>;
    getBoundsCenter(bounds): {
        latitude: number;
        longitude: number;
    };
    getBoundsForRadius(lat, lon, radius): {
        north: number;
        south: number;
        east: number;
        west: number;
    };
    isWithinBounds(lat, lon, bounds): boolean;
    parseCoordinate(coordinate): {
        latitude: number;
        longitude: number;
    };
    toDeg(radians): number;
    toRad(degrees): number;
    watchPosition(callback, options?): number;
} = ...

Type declaration

  • calculateBearing:function
    • Calculates the bearing (direction) between two geographic coordinates using the Haversine formula.

      Parameters

      • lat1: number

        The first latitude in degrees

      • lon1: number

        The first longitude in degrees

      • lat2: number

        The second latitude in degrees

      • lon2: number

        The second longitude in degrees

      Returns number

      The bearing in degrees (0-360) from north

      Example

      // 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°"
  • calculateDestination:function
    • Calculates a new coordinate point based on a starting point, distance, and bearing using the Haversine formula.

      Parameters

      • lat: number

        The starting latitude in degrees

      • lon: number

        The starting longitude in degrees

      • distance: number

        The distance to travel in kilometers

      • bearing: number

        The bearing (direction) in degrees (0-360)

      Returns {
          latitude: number;
          longitude: number;
      }

      An object containing the new latitude and longitude coordinates

      • latitude: number
      • longitude: number

      Example

      // 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)}`);
  • calculateDistance:function
    • Calculates the great-circle distance between two geographic coordinates using the Haversine formula.

      Parameters

      • lat1: number

        The first latitude in degrees

      • lon1: number

        The first longitude in degrees

      • lat2: number

        The second latitude in degrees

      • lon2: number

        The second longitude in degrees

      Returns number

      The distance in kilometers

      Example

      // 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"
  • clearWatch:function
    • Stops watching the user's position that was previously started with watchPosition.

      Parameters

      • watchId: number

        The watch ID returned by watchPosition

      Returns void

      Throws

      If geolocation is not supported by the browser

      Example

      // 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');
  • formatCoordinate:function
    • Formats geographic coordinates as a string in either decimal degrees (DD) or degrees, minutes, seconds (DMS) format.

      Parameters

      • lat: number

        The latitude in degrees

      • lon: number

        The longitude in degrees

      • format: "DMS" | "DD" = "DD"

        The format to use: 'DD' for decimal degrees or 'DMS' for degrees, minutes, seconds

      Returns string

      A formatted coordinate string

      Example

      // 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"
  • getCurrentPosition:function
    • Gets the user's current position using the browser's Geolocation API.

      Parameters

      • options: PositionOptions = ...

        Geolocation options for accuracy, timeout, and maximum age

      Returns Promise<GeolocationPosition>

      A Promise that resolves with the GeolocationPosition object

      Throws

      If geolocation is not supported by the browser

      Example

      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);
      }
  • getBoundsCenter:function
    • Calculates the center point of a geographic bounding box.

      Parameters

      • bounds: {
            north: number;
            south: number;
            east: number;
            west: number;
        }

        The bounding box with north, south, east, and west coordinates

        • north: number
        • south: number
        • east: number
        • west: number

      Returns {
          latitude: number;
          longitude: number;
      }

      An object containing the latitude and longitude of the center point

      • latitude: number
      • longitude: number

      Example

      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"
  • getBoundsForRadius:function
    • Calculates a bounding box for a given radius around a center point.

      Parameters

      • lat: number

        The center latitude in degrees

      • lon: number

        The center longitude in degrees

      • radius: number

        The radius in kilometers

      Returns {
          north: number;
          south: number;
          east: number;
          west: number;
      }

      A bounding box with north, south, east, and west coordinates

      • north: number
      • south: number
      • east: number
      • west: number

      Example

      // 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
  • isWithinBounds:function
    • Checks if a coordinate point is within a geographic bounding box.

      Parameters

      • lat: number

        The latitude to check in degrees

      • lon: number

        The longitude to check in degrees

      • bounds: {
            north: number;
            south: number;
            east: number;
            west: number;
        }

        The bounding box with north, south, east, and west coordinates

        • north: number
        • south: number
        • east: number
        • west: number

      Returns boolean

      True if the coordinate is within the bounds, false otherwise

      Example

      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
  • parseCoordinate:function
    • Parses a coordinate string in decimal degrees format into latitude and longitude values.

      Parameters

      • coordinate: string

        The coordinate string in "latitude, longitude" format

      Returns {
          latitude: number;
          longitude: number;
      }

      An object containing the parsed latitude and longitude

      • latitude: number
      • longitude: number

      Throws

      If the coordinate string is invalid or cannot be parsed

      Example

      // 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"
      }
  • toDeg:function
    • Converts an angle from radians to degrees.

      Parameters

      • radians: number

        The angle in radians

      Returns number

      The angle in degrees

      Example

      const degrees = LocationUtils.toDeg(Math.PI);
      console.log(`${degrees}°`); // "180°"

      const halfCircle = LocationUtils.toDeg(Math.PI / 2);
      console.log(`${halfCircle}°`); // "90°"
  • toRad:function
    • Converts an angle from degrees to radians.

      Parameters

      • degrees: number

        The angle in degrees

      Returns number

      The angle in radians

      Example

      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)
  • watchPosition:function
    • Starts watching the user's position using the browser's Geolocation API.

      Parameters

      • callback: ((position) => void)

        The callback function to be called with position updates

          • (position): void
          • Parameters

            • position: GeolocationPosition

            Returns void

      • options: PositionOptions = ...

        Geolocation options for accuracy, timeout, and maximum age

      Returns number

      A watch ID that can be used to stop watching with clearWatch

      Throws

      If geolocation is not supported by the browser

      Example

      // 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);