Skip to content

Commit

Permalink
Added distance calculation for past trips. It is really something tha…
Browse files Browse the repository at this point in the history
…t should be returned from the API, but this gets the hard work done. The calculation for total distance is very hack-ish.
  • Loading branch information
stephenyeargin committed Oct 22, 2011
1 parent 36eea1e commit e8a3855
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
95 changes: 95 additions & 0 deletions includes/class.geocalculator.php
@@ -0,0 +1,95 @@
<?php
/**
* GeoCalculator
*/
class GeoCalculator {

static $earth_radius = 3960.00; // Measured in miles
static $GoogleMapsAPIKey = ''; // If you need to register your requests

/**
* Get Geo Distance
*
* @param string $lat1
* @param string $lon1
* @param string $lat2
* @param string $lon2
* @return int
*/
public function getGeoDistance($lat1=0, $lon1=0, $lat2=0, $lon2=0) {

// If either set is not set, distance is automatically zero.
if ( (!$lat1 && !$lon1) || (!$lat2 && !$lon2) )
return 0;

return self::_distance_haversine($lat1, $lon1, $lat2, $lon2);
}

/**
* Geocode an Address
*
* @param string $address
*/
public function geocodeAddress($address) {

if (!$address)
return false;

$api_key = self::$GoogleMapsAPIKey;
$address = urlencode($address);
$url = sprintf("http://maps.google.com/maps/geo?q=%s&key=%s&output=json", $address, $api_key);

/* cURL request */
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);

if (!$data)
return false;

// Decode the JSON response
$json = json_decode($data);

if(!isset($json->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;
}

}
5 changes: 3 additions & 2 deletions includes/master.inc.php
Expand Up @@ -8,6 +8,7 @@
require 'class.tripit.php';
require 'class.smarty.php';
require 'class.swift.php';
require 'class.geocalculator.php';

// Globals
session_start();
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions index.php
Expand Up @@ -6,13 +6,40 @@
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
$Smarty->assign('past_trips', array($past_trips->Trip));
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
Expand Down
7 changes: 7 additions & 0 deletions templates/main.tpl
Expand Up @@ -47,13 +47,17 @@


<h2>Past Trips</h2>
<p><strong>Estimated Miles Traveled:</strong> {$miles_traveled|number_format} [<a href="javascript:void(0)" onclick="javascript:alert('Calculated between your home city and primary destination, multipled by two to get to/from distance.')">?</a>]</p>
<table>
<thead>
<tr>
<th>Trip</th>
<th>City</th>
<th>Start Date</th>
<th>End Date</th>
{if $track_miles eq true}
<th>Distance (miles)</th>
{/if}
</tr>
</thead>
<tbody>
Expand All @@ -63,6 +67,9 @@
<td class="primary-location">{$trip->primary_location}</td>
<td class="start-date">{$trip->start_date|date_format}</td>
<td class="end-date">{$trip->end_date|date_format}</td>
{if $track_miles eq true}
<td>{$trip->distance_traveled|number_format}</td>
{/if}
</tr>
{/foreach}
{if $past_trips|count eq 0}
Expand Down

0 comments on commit e8a3855

Please sign in to comment.