So, I want to do the following:
- In a .each function, push the first object into an array X
- Execute a function that compares 2nd object with the first already in, and if result < 100, push it, otherwise leave it out.
- Do the same with the 3rd object (comparing with latest object in), 4th object (comparing with latest object in), nth object (comparing with n-1)...
At the end I want to have an array containing only objects whose comparison with the latest pushed object is < 100.
I imagine something like this:
$(json.locations).each(function(key, value){
var tempLocation = new L.LatLng(latitude, longitude);
if (locationArray.length < 1){ // push first object
locationArray.push(tempLocation);
counter++;
}
else{ // first object is in, the rest will be conditional on comparision
if (execute function, get result, if result > 100){
locationArray.push(tempLocation);
counter++;
}
}
Am I going in the right direction? Any tips?
The function to compare the objects looks like this:
$(document).ready(function (wop) {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
var lat2 = 42.741;
var lon2 = -71.3161;
var lat1 = 42.806911;
var lon1 = -71.290611;
var R = 637100; // km
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();
var x2 = lon2-lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
if (d < 100){
alert("This is close! " + d + " meters!");
}
else{
alert("Far away!");
}
});
Where lat1, lat2 and lng1, lng2 are the values to be fetched from the last object in and the one being compared.
EDIT
Using the answer from iam-decoder I've gotten here:
var testLocation = function(location){
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
var lat1 = location.lat;
var lon1 = location.lng;
var lat2 = 42.806911;
var lon2 = -71.290611;
var R = 637100; // km
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();
var x2 = lon2-lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var distanceresult = R * c;
return distanceresult;
console.log(distanceresult); // not working??
};
// iterate through the locations and create map markers for each location
$(json.locations).each(function(key, value){
var latitude = $(this).attr('latitude');
var longitude = $(this).attr('longitude');
var tempLocation = new L.LatLng(latitude, longitude);
if (locationArray.length < 1){
locationArray.push(tempLocation);
counter++;
}
else if(testLocation(tempLocation, locationArray[locationArray.length-1]) >= 100){
console.log("good"); // tested the else if and it works.
}
});
console.log(locationArray)
I tested the else if expression and it works (adding return 100 and return 101 in testlocation function).
The problem now is that I don't really know how to pass the latitudes/longitudes from the last element in the array and from the new candidate into the testlocation function to make the calculation.
How do I pass these variables from one function to the other??
locationArray.length > 1
? First argument is the n-th object and second is the n-1-th object? I suggest you do declare the function in the second code sample seperately and return a float (distance calculated). This float can easely checked for < 100