I am currently working on developing a geolocation test app.
After finding some useful code for calculating the distance between two points using latitude and longitude coordinates, I am attempting to create a loop using a for loop to calculate the distance between multiple points.
For example, if I have a reference point X and several other points A, B, etc., I want to calculate the distance between X and each of these points.
The longitude and latitude coordinates of the other points are stored in an associative-dimensional array like this:
var response = (
{
id: 2,
latitude: "50",
longitude: "0",
username: AAA
},
{
id: 5,
latitude: "51",
longitude: "-1",
username: BBB
},
{
id: 6,
latitude: "52",
longitude: "-3",
username: CCC
}
)
// Point X data:
var lat1 = "50";
var lon1 = "0";
My goal is to:
for(var i = 0; i < response.length; i++){
var lat2 = response[i].latitude;
var lon2 = response[i].longitude;
function distance(lat1, lon1, lat2, lon2,response) {
// Calculation for distance in kilometers
// ...
}
}
However, when I run this code and output it, the 'distanceKm' value for, say, response[1] is coming out as 'Undefined'. Something seems to be going wrong, but I cannot figure it out. Any suggestions?
Thank you in advance.