diff --git a/includes/class.geocalculator.php b/includes/class.geocalculator.php new file mode 100644 index 0000000..2f33b10 --- /dev/null +++ b/includes/class.geocalculator.php @@ -0,0 +1,95 @@ +Status->code) || $json->Status->code != "200") + return false; + + // Parse coordinates into latitude and logitude + return ($json->Placemark[0]->Point->coordinates); + + } + + /* Private Methods */ + + /** + * Constructor + */ + private function __construct() { + // Not an instatiated object. + } + + /** + * Haversine Distance Calculation + * + * @param string $lat1 + * @param string $lon1 + * @param string $lat2 + * @param string $lon2 + * @return int + */ + private function _distance_haversine($lat1, $lon1, $lat2, $lon2) { + + // Calculate Deltas + $delta_lat = $lat2 - $lat1 ; + $delta_lon = $lon2 - $lon1 ; + + $alpha = $delta_lat/2; + $beta = $delta_lon/2; + $a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ; + $c = asin(min(1, sqrt($a))); + $distance = 2*self::$earth_radius * $c; + $distance = round($distance, 4); + + return (float) $distance; + } + +} \ No newline at end of file diff --git a/includes/master.inc.php b/includes/master.inc.php index a6fb397..f74f464 100644 --- a/includes/master.inc.php +++ b/includes/master.inc.php @@ -8,6 +8,7 @@ require 'class.tripit.php'; require 'class.smarty.php'; require 'class.swift.php'; +require 'class.geocalculator.php'; // Globals session_start(); @@ -35,8 +36,8 @@ $TripIt = new TripItShare( $token ); // Profile - $profile = $TripIt->GetProfile(); - $Smarty->assign('profile', $profile->Profile); + $Profile = $TripIt->GetProfile(); + $Smarty->assign('profile', $Profile->Profile); else: // Don't process redirect if already on OAuth page diff --git a/index.php b/index.php index c15d785..b8abbb9 100644 --- a/index.php +++ b/index.php @@ -6,6 +6,9 @@ if (!$TripIt) header('Location: oauth.php'); +// Total Distance Traveled +$total_distance = 0; + // Past Trips $past_trips = $TripIt->GetTrips( array('past' => 'true') ); if (isset($past_trips->Trip->display_name)) // if single trip @@ -13,6 +16,30 @@ else $Smarty->assign('past_trips', $past_trips->Trip ); +/* + Calculating a Simple Distance between 'home' and + the primary location of the trip. Proof of concept. +*/ +$track_miles = true; +$home_city = $Profile->Profile->home_city; +if (!$home_city) + $track_miles = false; // Can't calculate without city/state + +if ($track_miles): + $home_city_geo = GeoCalculator::geocodeAddress($home_city); + foreach ($past_trips->Trip as $key => $trip): + $lat1 = $trip->PrimaryLocationAddress->latitude; + $lon1 = $trip->PrimaryLocationAddress->longitude; + $distance = GeoCalculator::getGeoDistance($lat1, $lon1, $home_city_geo[0], $home_city_geo[1]); + $past_trips->Trip[$key]->distance_traveled = $distance; + $total_distance += ($distance * 2); + endforeach; + $Smarty->assign('miles_traveled', $total_distance); +else: + $Smarty->assign('miles_traveled', 0); +endif; +$Smarty->assign('track_miles', (bool) $track_miles); + // Future Trips $future_trips = $TripIt->GetTrips( array('past' => 'false') ); if (isset($future_trips->Trip->display_name)) // if single trip diff --git a/templates/main.tpl b/templates/main.tpl index ab059a9..f33295b 100644 --- a/templates/main.tpl +++ b/templates/main.tpl @@ -47,6 +47,7 @@

Past Trips

+

Estimated Miles Traveled: {$miles_traveled|number_format} [?]

@@ -54,6 +55,9 @@ + {if $track_miles eq true} + + {/if} @@ -63,6 +67,9 @@ + {if $track_miles eq true} + + {/if} {/foreach} {if $past_trips|count eq 0}
City Start Date End DateDistance (miles)
{$trip->primary_location} {$trip->start_date|date_format} {$trip->end_date|date_format}{$trip->distance_traveled|number_format}