Skip to content

Instantly share code, notes, and snippets.

@russ
Created December 14, 2010 18:52
Show Gist options
  • Save russ/740872 to your computer and use it in GitHub Desktop.
Save russ/740872 to your computer and use it in GitHub Desktop.
function OnLoad() {
try {
var distances = [];
if (google.loader.ClientLocation) {
for (var i in cities) {
lat1 = cities[i].lat;
lon1 = cities[i].lng;
dLat = (google.loader.ClientLocation.latitude - lat1).toRad();
dLon = (google.loader.ClientLocation.longitude - lon1).toRad();
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1.toRad()) * Math.cos(google.loader.ClientLocation.latitude.toRad()) * Math.sin(dLon / 2) * Math.sin(dLon
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));·
distances.push(r * c);
}
window.location = cities[Array.min(distances)].url;
} else {
window.location = cities[0].url;
}
} catch(err) {
window.location = cities[0].url;
}
}
google.setOnLoadCallback(OnLoad);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment