/** * @param $id * @return bool|\stdClass */ function get_activity($id) { $map_return = new stdClass(); $activity = $this->strava_get('/activities/' . $id); if (empty($activity->map->polyline)) { return FALSE; } else { $points = decodePolylineToArray($activity->map->polyline); if (empty($points)) { return FALSE; } $businesses = array(); $last_api_point = null; // Collect businesses along the route every YELP_METRES_PER_CALL metres. foreach ($points as $point) { if (empty($last_api_point) || $this->haversineGreatCircleDistance($last_api_point, $point) > self::YELP_METRES_PER_CALL) { $last_api_point = $point; $locations = $this->get_locations($point); // Build an array of unique business locations. if (!empty($locations) && !empty($locations->businesses)) { foreach ($locations->businesses as $business) { if (empty($businesses[$business->id])) { $businesses[$business->id] = $business; } } } } } // For each point on the route, check the distance to each business and store // the lowest distance for each. foreach ($points as $point) { foreach ($businesses as &$business) { $distance = $this->haversineGreatCircleDistance(array($business->location->coordinate->latitude, $business->location->coordinate->longitude), $point); if (empty($business->stracof_distance) || $distance < $business->stracof_distance) { $business->stracof_distance = round($distance); } } } $total_points = 0; // Calculate distance under YELP_MAX_METRES_FOR_POINTS and multiply by points // value minus YELP_NEUTRAL_RATING to give a positive or negative score per business. foreach ($businesses as &$business) { $distance = self::YELP_MAX_METRES_FOR_POINTS - $business->stracof_distance; if ($distance > 0 && $business->rating > 0) { $points = $business->rating - self::YELP_NEUTRAL_RATING; $business->stracof_points = round($points * $distance); $total_points += $business->stracof_points; } } // Return the polyline, total and array values for businesses, as it won't // be an array in JS if we leave the array keys in place. $map_return->polyline = $activity->map->polyline; $map_return->businesses = array_values($businesses); $map_return->total_points = $total_points; } return $map_return; }
* The encoding algorithm is detailed here: * http://code.google.com/apis/maps/documentation/polylinealgorithm.html * * This function is based off of Mark McClure's JavaScript polyline decoder * (http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.js) * which was in turn based off Google's own implementation. * * This function assumes a validly encoded polyline. The behaviour of this * function is not specified when an invalid expression is supplied. * * @param String $encoded the encoded polyline. * @return Array an Nx2 array with the first element of each entry containing * the latitude and the second containing the longitude of the * corresponding point. */ $arr = decodePolylineToArray('zebeCzes|Gx@Ma@oCs@qEe@qCe@wC}@yFiAmHw@uFe@wCe@oCPSLiCTmCTyCd@mEVsDn@_Gd@eCLaCcB_ByEuCgCoD{@qCQwDLcEl@aFx@uDxAkDXmBq@wEeA_GgAeF{ATwBwCa@qAqJuLmG{IaBaCwB{BUw@sAeBc@kIi@uJUwCiA_D_AkFq@gAi@qDoAcAq@TgA['); print_r($arr); function decodePolylineToArray($encoded) { $length = strlen($encoded); $index = 0; $points = array(); $lat = 0; $lng = 0; while ($index < $length) { // Temporary variable to hold each ASCII byte. $b = 0; // The encoded polyline consists of a latitude value followed by a // longitude value. They should always come in pairs. Read the // latitude value first. $shift = 0;