0

I have a selection of input boxes that contain latitude and longitude values. I have created scipr that iterates over those input boxes and grabs those values for use in calculations. The calculations in question are converstion to Radians and then working out distances between points.

I grab this data using and store it in an object array like so:

$('.plotrow').each(function () {
    obj[this.id] = {
        lat: $(this).find('.lat input').val(),
        lon: $(this).find('.lon input').val()
    };
});

I would like the script to ignore any input value that's returned that is null or empty string. To save time I don't want to have to explicitly define each item in the array and chack it, is there any way I can do this with something like a recursive loop? If so can anyone provide an example of this?

Here is what I do with the cost after I grab the values. This code performs some maths functions to work out distances, if any value is null it breaks my code which is why I need to root out null values and stop it trying to use them.

plot1LatRad = deg2rad(obj.plot1.lat);
plot1LonRad = deg2rad(obj.plot1.lon);

plot2LatRad = deg2rad(obj.plot2.lat);
plot2LonRad = deg2rad(obj.plot2.lon);

plot3LatRad = deg2rad(obj.plot3.lat);
plot3LonRad = deg2rad(obj.plot3.lon);

plot4LatRad = deg2rad(obj.plot4.lat);
plot4LonRad = deg2rad(obj.plot4.lon);

//A => B
var AtoBLat = plot2LatRad - plot1LatRad;
var AtoBLon = plot2LonRad - plot1LonRad;

//B => C
var BtoCLat = plot3LatRad - plot2LatRad;
var BtoCLon = plot3LonRad - plot2LonRad;

//C => D
var CtoDLat = plot4LatRad - plot3LatRad;
var CtoDLon = plot4LonRad - plot3LonRad;

AtoBSum = Math.pow(Math.sin(AtoBLat / 2), 2) + Math.cos(plot1LatRad) * Math.cos(plot2LatRad) * Math.pow(Math.sin(AtoBLon / 2), 2);
BtoCSum = Math.pow(Math.sin(BtoCLat / 2), 2) + Math.cos(plot2LatRad) * Math.cos(plot3LatRad) * Math.pow(Math.sin(BtoCLon / 2), 2);
CtoDSum = Math.pow(Math.sin(CtoDLat / 2), 2) + Math.cos(plot3LatRad) * Math.cos(plot4LatRad) * Math.pow(Math.sin(CtoDLon / 2), 2);

AtoBSqrt = 2 * Math.atan2(Math.sqrt(AtoBSum), Math.sqrt(1 - AtoBSum));
BtoCSqrt = 2 * Math.atan2(Math.sqrt(BtoCSum), Math.sqrt(1 - BtoCSum));
CtoDSqrt = 2 * Math.atan2(Math.sqrt(CtoDSum), Math.sqrt(1 - CtoDSum));
Yanayaya
  • 2,044
  • 5
  • 30
  • 67

0 Answers0