I have a JSON
array that is being populated with lat/long pairs among some other data between periods of 3
minutes.
I want to calculate the distances between each of the elements using a haversine function which takes two pairs of coordinates (start point and end point).
How can I loop through the array in order to calculate the distance between element A
and B
, then B
and C
, C -> D
, D -> E
and so on...
Here is how my JSON
looks like:
{
"data":[
{
"latitude":37.80,
"longitude":-121.493300,
"report_date":"2019-07-01 12:00:00"
},
{
"latitude":37.80,
"longitude":-121.493300,
"report_date":"2019-07-01 12:03:00"
},
{
"latitude":37.80,
"longitude":-121.493300,
"report_date":"2019-07-01 12:06:00"
}
]
}
I haven't tried much other than just trying to start a for
loop and manipulating some of the other data that I didn't show in the JSON
example (that other data is irrelevant)
I think this is gonna be much more complicated than using a for
loop, perhaps there is some other JS
feature that could help me accomplish this? I need a way to store that iteration variable and make an exception for the first iteration?
var i = 0
data.forEach(element => {
var current_location = {lat: element.latitude, lon:element.longitude}
i++
});
For every iteration I would like to be able to call my haversine(pair1, pair2)
function and get the distance, I could now perform other operations like calculating speed, etc...