所以,我想做以下事情:
最后,我希望有一个数组,其中只包含与最新推送对象的比较为< 100。
我想象这样的事情:
$(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++;
}
}
我是朝着正确的方向前进的吗?有什么提示吗?
比较对象的函数如下所示:
$(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!");
}
});
其中 lat1,lat2和lng1,lng2 是要从最后一个对象和被比较的对象中提取的值。
使用 iam-decoder 的答案我已经来到这里:
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)
我测试了else if if表达式并且它有效(在testlocation函数中添加return 100并返回101)。
现在的问题是我真的不知道如何将数组中的最后一个元素和新的候选中的纬度/经度传递到testlocation函数中进行计算
如何将这些变量从一个函数传递给另一个函数?
答案 0 :(得分:1)
只需创建用于比较的匿名函数并将其存储在变量中:
var testLocation = function(location, compareTo){
var result;
//do something
return result;
};
$(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 if(testLocation(tempLocation, locationArray[locationArray.length-1]) > 100){
locationArray.push(tempLocation);
counter++;
}
});
答案 1 :(得分:-1)
据我所知,您希望过滤LonLats与它们之间的距离。
我想象这样的事情:
var maxDistance = 100;
var filtered = [];
var compareAndPush = function(item) {
if (filtered.length == 0) {
filtered.push(item);
} else {
var distance = compareLonLats(item, filtered[filtered.length]);
if (distance < maxDistance) {
filtered.push(item);
}
}
}
$(json.locations).each(compareAndPush);
另外将比较过程放入一个名为&#39; compareLonLats&#39;的单独函数中。或类似的东西。