Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongrover/6056123 to your computer and use it in GitHub Desktop.
Save jongrover/6056123 to your computer and use it in GitHub Desktop.
This Gist shows an example where you can hard code starting address and ending address locations and the script will tell you the closest stations to your starting and ending trip points. Included as dependencies are jQuery and Google Maps API.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&amp;sensor=false"></script>
<script>
// Set starting addresses
var start_address = 'Union square NYC',
end_address = 'Times Square NYC',
start_station = '',
end_station = '';
console.log('start address: '+start_address);
console.log('end address: '+end_address);
// Function to convert addresses to lat,lng
function getLatLng(start, end, callback) {
var geocoder = new google.maps.Geocoder();
// geocode the start
geocoder.geocode({'address': start}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var startlat = results[0].geometry.location.lat();
var startlng = results[0].geometry.location.lng();
console.log('start latitude: '+startlat+', start longitude: '+startlng);
// geocode the end
geocoder.geocode({'address': end}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var endlat = results[0].geometry.location.lat();
var endlng = results[0].geometry.location.lng();
console.log('end latitude: '+endlat +', end longitude: '+endlng);
//return [startlat, startlng, endlat, endlng];
callback(startlat, startlng, endlat, endlng);
}
});
}
});
}
// Function to get distance between points
function getDistance(lat1,lng1,lat2,lng2) {
//console.log('lat1: '+lat1+' is an: '+typeof lat1); // lat1: (40.759011, -73.98447220000003) is an: object
//console.log('lng1: '+lng1+' is an: '+typeof lng1); // lng1: undefined is an: undefined
//console.log('lat2: '+lat2+' is an: '+typeof lat2); // lat2: 40.76727216 is an: number
//console.log('lng2: '+lng2+' is an: '+typeof lng2); // lng2: -73.99392888 is an: number
var i = lat1.jb - lat2;
var j = lat1.kb - lng2;
//console.log(i);
//console.log(j);
var formula = i*i + j*j;
return formula;
}
// Function to find the nearest station
function findNearestStation(lat,lng) {
var min_distance = 99999,
closest_station_id = 0;
$.getJSON('stations.json', function(stations) {
//console.log(stations);
$.each(stations.stationBeanList, function(i, station) {
var distance = getDistance(lat,lng, station.latitude, station.longitude); //call get distance
//console.log(distance);
if (distance < min_distance) {
min_distance = distance;
closest_station_id = i;
}
});
console.log(closest_station_id);
console.log(stations.stationBeanList[closest_station_id].stationName+' is the closest station.');
return stations.stationBeanList[closest_station_id];
});
}
// Call the getLatLng function to run and pass in callback function
getLatLng(start_address, end_address, function(start_lat, start_lng, end_lat, end_lng) {
var start_p = new google.maps.LatLng(start_lat, start_lng),
end_p = new google.maps.LatLng(end_lat, end_lng);
start_station = findNearestStation(start_p); //call nearest station
end_station = findNearestStation(end_p); //call nearest station
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment