Created
May 28, 2010 19:28
-
-
Save kirbysayshi/417612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Crazy Haversine formula for getting locations within 25 miles of searched location. | |
//Don't touch this or it will explode! | |
function hvs1(results, Rga){ | |
var R = 6371; // Radius of the earth in km | |
var dLat = (results[0].geometry.location.b - Rga.FoodBankData[0].Latitude).toRad(); // Javascript functions in radians | |
var dLon = (results[0].geometry.location.c - Rga.FoodBankData[0].Longitude).toRad(); | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(Rga.FoodBankData[0].Latitude.toRad()) * Math.cos(results[0].geometry.location.b.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; // Distance in km | |
return d; | |
} | |
function hvs2(latA, longA, latB, longB){ | |
var dLat = (latA - latB).toRad(); | |
var dLon = (longA - longB).toRad(); | |
var dLatDiv2 = dLat / 2; | |
var dLonDiv2 = dLon / 2; | |
var latBRad = latB.toRad(); | |
var latBRadCos = Math.cos(latBRad); | |
var dLatDiv2Sin = Math.sin(dLatDiv2); | |
var dLonDiv2Sin = Math.sin(dLonDiv2); | |
var a = dLatDiv2Sin * dLatDiv2Sin + latBRadCos * latBRadCos * dLonDiv2Sin * dLonDiv2Sin; | |
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
} | |
function hvs3(latA, longA, latB, longB){ | |
var a = Math.sin((latA - latB).toRad()/ 2) * Math.sin((latA - latB).toRad() / 2) + | |
Math.cos(latB.toRad()) * Math.cos(latB.toRad()) * | |
Math.sin((longA - longB).toRad() / 2) * Math.sin((longA - longB).toRad() / 2); | |
var d = 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // 6371 Radius of the earth in km | |
return d; | |
} | |
if (typeof (Number.prototype.toRad) === "undefined") { | |
Number.prototype.toRad = function() { | |
return this * 3.14 / 180; | |
} | |
} | |
function bench(methodName, param1, param2, param3, param4){ | |
var start = new Date(); | |
var i = 0; | |
for(i = 0; i < 40000; i++){ | |
methodName(param1, param2, param3, param4); | |
} | |
var end = new Date(); | |
console.log(end.getTime() - start.getTime()); | |
} | |
var results = [ | |
{ | |
geometry: { | |
location: { b: 10, c: 10 } | |
} | |
} | |
]; | |
var Rga = { | |
FoodBankData: [ | |
{ Latitude: 20, Longitude: 20 } | |
] | |
}; | |
bench( hvs1, results, Rga ); | |
bench( hvs2, 20, 20, 10, 10 ); | |
bench( hvs3, 20, 20, 10, 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment