Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Trip

A single Trip defines a path from one point to another. It returns the vehicle position and heading based on a specific speed, a start date and the current date.

// From - To. The two points are 10km appart.
const a = new google.maps.LatLng(20.65273901635008, -103.44528316226638)
const b = new google.maps.LatLng(20.693668019183118, -103.35969321162369)

// Creating new Trip:
// - 190° start heading
// - 10 km/h speed
const trip = new Mapster.Trip(a, b, 190, 10)

// Printing the distance our vehicle has traveled each 15 minutes. Since we are doing 10km at 10km/h we should be getting 2.5km every 15 minutes.
const startTimestamp = new Date('2000-01-01 08:00').getTime()
const splits = []
new Array(5).fill(0).forEach((t, i) => {
    const currentTimestamp = startTimestamp + (15 * 1000 * 60) * i
    const distance = trip.getTraveledDistance(startTimestamp, currentTimestamp)
    const position = trip.getPositionInTime(startTimestamp, currentTimestamp)
    const heading = trip.getHeadingInTime(startTimestamp, currentTimestamp)
    splits.push({
        time: new Date(currentTimestamp).toLocaleTimeString(),
        distance: (distance / 1000).toFixed(1),
        lat: position.lat().toFixed(2),
        lng: position.lng().toFixed(2),
        heading: heading.toFixed(0),
    })
})

console.table(splits)

Table Result

As you can see, the first snapshot still has a 190 heading angle but second snapshot does not. That's because the trip mannager first task is to move the vehicle heading angle in point B's direction. It might take a houndrer of milliseconds to get to the final heading angle.

Hierarchy

  • Trip

Index

Constructors

constructor

  • new Trip(from: LatLng, to: LatLng, initialHeading: number, speed: number, animate?: boolean): Trip
  • Creates a new Trip

    Parameters

    • from: LatLng

      Initial position.

    • to: LatLng

      Final position.

    • initialHeading: number

      Initial heading in degrees.

    • speed: number

      Speed in km/h.

    • Default value animate: boolean = true

    Returns Trip

Accessors

angleDiff

  • get angleDiff(): number
  • Returns number

distance

  • get distance(): number
  • Returns number

duration

  • get duration(): number
  • Returns number

from

  • get from(): LatLng
  • Returns LatLng

heading

  • get heading(): number
  • Returns number

initialHeading

  • get initialHeading(): number
  • Returns number

rotationDuration

  • get rotationDuration(): number
  • Returns number

speed

  • get speed(): number
  • Returns number

to

  • get to(): LatLng
  • Returns LatLng

Methods

getHeadingInTime

  • getHeadingInTime(startTimeStamp: number, timeStamp: number): number
  • Returns the heading on a particular point in time.

    Parameters

    • startTimeStamp: number

      When the trip was supposed to start.

    • timeStamp: number

      Current time.

    Returns number

    Vehicle's current heading in degrees.

getPositionInTime

  • getPositionInTime(startTimeStamp: number, timeStamp: number): LatLng
  • Returns the position on a particular point in time.

    Parameters

    • startTimeStamp: number

      When the trip was supposed to start.

    • timeStamp: number

      Current time.

    Returns LatLng

    Vehicle's current position.

getTraveledDistance

  • getTraveledDistance(startTimeStamp: number, timeStamp: number): number
  • Returns the total traveled distance on a particular point in time.

    Parameters

    • startTimeStamp: number

      When the trip was supposed to start.

    • timeStamp: number

      Current time.

    Returns number

    Vehicle's current traveled distance.

isTripOver

  • isTripOver(startTimeStamp: number, timeStamp: number): boolean
  • Whether the trip has ended based on an specific point in time.

    Parameters

    • startTimeStamp: number

      When the trip was supposed to start.

    • timeStamp: number

      Current time.

    Returns boolean

    Whether is has ended or not.

Generated using TypeDoc