How can I get the next item in an array.

Say I have a set or coordinates as markers in an array,

Array 1 = lat1, lat2, lat3, etc

Array 2 = lon1, lon2, lon3, etc

I want to show the distance and bearing of each point one at a time so say for example upon reaching say lat 2 and lon 2 I would like the array to show the distance and bearing to lat 3, lon3, disregarding the others you’ve already reached.

I have a rough idea of something like this

var n = 0;

if (n != array1.size -1) {

var nextObj = array1[n], array2[n];

if (nextObj = currLoc); (Give or take a few metres but not sure how to do that)

n = array1[n+1], array2[n+1];

until you reach the end of the array}

would this code work?

  • Find the distance to the next waypoint and if its below a threshold progress to the next,

    function GetDistance(latStart, lonStart, latEnd, lonEnd)
    	{
        	var LatDiff = latEnd - latStart;
      		var LonDiff = lonEnd - lonStart;
    
    		var a = Math.pow(Math.sin(LatDiff/2), 2) +
      		Math.pow(Math.sin(LonDiff/2), 2) * Math.cos(latStart) * Math.cos(latEnd);
     		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      		var distance = EARTH_RADIUS_M * c;
      		return distance;
    	}

    Note there is a Const defined as;

     const EARTH_RADIUS_M = 6378137;

    if (distance2Wp < 1000) 
    		{
    			changeCurrentWp(_currentWayPoint + 1);
    		}