skip to Main Content

Any ideas on how to calculate the nautical miles using react? Assume that I have an object:

const calculateDistance = [
    {point_id: 1, long: longitude , lat: latitude},
    {point_id: 2, long: longitude , lat: latitude},
    {point_id: 3, long: longitude , lat: latitude},
    {point_id: 4, long: longitude , lat: latitude},
    {point_id: 5, long: longitude , lat: latitude},
]

I would greatly appreciate any suggestions. Thank you!

2

Answers


  1. React is a rendering library so you don’t calculate in React. However, you are using javascript or typescript and this you can use. Something like:

    function getDistanceFromLatLonInNauticalMiles(lat1, lon1, lat2, lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = deg2rad(lat2-lat1);  // deg2rad below
      var dLon = deg2rad(lon2-lon1); 
      var a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ; 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km
      return d * 0.539957; // convert to nautical miles
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI/180)
    }
    

    to calculate between 2 points using the https://en.wikipedia.org/wiki/Haversine_formula then you can add them if you need multiple points.

    Login or Signup to reply.
  2. You calculate the distance using the haversine formula in react. Here is how:

    import React, { useState } from "react";
    import haversine from "haversine"; //haversine library 
    
    const CalculateDistance = () => {
      const [calculateDistance, setCalculateDistance] = useState([
        { point_id: 1, longitude: -122.4194, latitude: 37.7749 }, //longitude and latitude changed
        { point_id: 2, longitude: -118.2437, latitude: 34.0522 },  //longitude and latitude changed
        { point_id: 3, longitude: -118.2437, latitude: 34.0522 }, //longitude and latitude changed
        { point_id: 4, longitude: -118.2437, latitude: 33.5522 }, //longitude and latitude changed
        { point_id: 5, longitude: -121.4194, latitude: 36.7749 } //changed longitude and latitude
      ]);
    
      // changed the function to calculateDistance function
      const calculateDistanceValues = () => {
        let totalDistance = 0;
        for (let i = 1; i < calculateDistance.length; i++) {
          const currentPoint = calculateDistance[i - 1];
          const nextPoint = calculateDistance[i];
    
          const distance = haversine(currentPoint, nextPoint, {
            //longitude and latitude values are used instead of unit 'mi'
            unit: "nmi",
          });
          console.log(distance)
          totalDistance += distance;
        }
    
        return totalDistance.toFixed(2); // distance need to be fixed to 2 decimal points
      };
    
      return <div>Total Distance: {calculateDistanceValues()} nm</div>; // distance need to have a  unit of nm
    };
    

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search