Source

utils/map.js

/**
 * Linestring example: 1.4659516666667 45.162342314815,1.4659516666667 45.162342314815
 * Polyline example: [[1.4659516666667,45.162342314815],[1.4659516666667,45.162342314815]]
 * @param {*} linestring
 */
export function getPolylineFromLinestring(linestring) {
  //APIV3: Remove 'LINESTRING(' at the beginning and ')' at the end
  if (linestring.includes('LINESTRING(')) {
    /*console.log('Parsing linestring', {
      linestring,
    })*/
    linestring = linestring.split('LINESTRING(').join('').split(')').join('')
    /* console.log('Parsing linestring::remove-characters', {
      linestring,
    })*/
  }

  let polyline =
    linestring === ''
      ? []
      : linestring
          .split(',')
          .map((latlng) => [
            parseFloat(latlng.trim().split(' ')[1]),
            parseFloat(latlng.trim().split(' ')[0]),
          ])
  /*console.log('Parsing linestring::result', {
    polyline,
  })*/
  return polyline
}

/**
 * Used by Diagnostics module to compute the vehicle marker bearing angle.
 *
 * Bearing from point A to B
 * https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/
 * @param {*} prevLatLng
 * @param {*} nextLatLng
 */
export function getBearingFromTwoLatAndLng(prevLatLng, nextLatLng) {
  let [lat1, lng1] = prevLatLng
  let [lat2, lng2] = nextLatLng
  let fLat = degreeToRadians(lat1)
  let fLong = degreeToRadians(lng1)
  let tLat = degreeToRadians(lat2)
  let tLong = degreeToRadians(lng2)
  let dLon = tLong - fLong
  let degrees = radiansToDegree(
    Math.atan2(
      Math.sin(dLon) * Math.cos(tLat),
      Math.cos(fLat) * Math.sin(tLat) -
        Math.sin(fLat) * Math.cos(tLat) * Math.cos(dLon)
    )
  )
  if (degrees >= 0) {
    return degrees
  } else {
    return 360 + degrees
  }
}

function degreeToRadians(latLong) {
  return (Math.PI * latLong) / 180.0
}

function radiansToDegree(latLong) {
  return (latLong * 180.0) / Math.PI
}