Created
August 21, 2011 19:20
-
-
Save adrianrego/1161029 to your computer and use it in GitHub Desktop.
Nearest Campus Demo
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
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<title>CF Locations</title> | |
</head> | |
<body> | |
<form action="" method="get" accept-charset="utf-8"> | |
<label for="zip">Enter your zipcode</label> | |
<input type="text" value="" id="zip" name="zip" /> | |
</form> | |
<button id="btn_find" name="btn_find">Find Nearest Campus</button> | |
<h3>Your nearest campus is: <span id="campus"></span></h3> | |
<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline --> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script> | |
<script type="text/javascript" charset="utf-8"> | |
var campuses = [ | |
{name: 'Downtown', coordinates:[25.778776,-80.192069]}, | |
{name: 'Homestead', coordinates:[25.479101,-80.419841]}, | |
{name: 'Palmetto Bay', coordinates:[25.613784,-80.337176]}, | |
{name: 'Redland', coordinates:[25.535786,-80.457498]}, | |
{name: 'West Kendall', coordinates:[25.687808,-80.394832]} | |
]; | |
function nearestCampus(data, status){ | |
var minDistance = 99999999; | |
var campus = null; | |
// Loop through elements and find the shortest distance | |
for(i=0; i < data.rows[0].elements.length; i=i+1){ | |
var distance = data.rows[0].elements[i].distance.value ; | |
if(distance < minDistance){ | |
minDistance = distance; | |
campus = campuses[i]; | |
} | |
} | |
// Set the name of the nearest campus | |
$('#campus').html(campus.name); | |
} | |
/* | |
* Use Google Maps Distance Matrix API to find the nearest campus based on zipcode | |
* http://code.google.com/apis/maps/documentation/javascript/services.html#distance_matrix | |
*/ | |
$('#btn_find').click(function(){ | |
var zip = $('#zip').val(); | |
var destinations = []; | |
var numCampuses = campuses.length; | |
for(i=0; i < numCampuses; i=i+1){ | |
destinations.push(new google.maps.LatLng(campuses[i].coordinates[0], campuses[i].coordinates[1])); | |
} | |
var service = new google.maps.DistanceMatrixService(); | |
service.getDistanceMatrix({ | |
origins: [zip], | |
destinations: destinations, | |
travelMode: google.maps.TravelMode.DRIVING, | |
}, nearestCampus); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you refuse to touch php.. hahah.