Modified formatting

Made Polyline PHP 5.2 compatible
This commit is contained in:
Justin Foell
2017-12-26 13:22:22 -06:00
parent abbdf7af95
commit 04cfda401a
5 changed files with 171 additions and 156 deletions
+38 -24
View File
@@ -1,7 +1,5 @@
<?php
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Polyline.php';
class WPStrava_StaticMap {
/**
@@ -11,10 +9,11 @@ class WPStrava_StaticMap {
*
* @static
* @access public
* @param object $ride Ride object from strava.
* @param int $height Height of map in pixels.
* @param int $width Width of map in pixels.
* @param bool $markers Display start and finish markers.
* @param object $ride Ride object from strava.
* @param int $height Height of map in pixels.
* @param int $width Width of map in pixels.
* @param bool $markers Display start and finish markers.
* @return string HTML img tag with static map image.
*/
public static function get_image_tag( $ride, $height = 320, $width = 480, $markers = false ) {
$key = WPStrava::get_instance()->settings->gmaps_key;
@@ -24,34 +23,49 @@ class WPStrava_StaticMap {
return '';
}
$url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:";
$url_len = strlen( $url );
$url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:";
$url_len = strlen( $url );
$max_chars = 1865;
$polyline = '';
if ( ! empty( $ride->map->polyline ) && ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) ) {
$url .= $ride->map->polyline;
$points = self::decode_polyline($ride->map->polyline);
$polyline = $ride->map->polyline;
} elseif ( ! empty( $ride->map->summary_polyline ) ) {
$url .= $ride->map->summary_polyline;
$points = self::decode_polyline($ride->map->summary_polyline);
$polyline = $ride->map->summary_polyline;
}
$url .= $polyline;
if ($markers) {
$markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] .
'&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1];
$url .= $markers;
}
if ( $markers ) {
$points = self::decode_start_finish( $polyline );
$markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] .
'&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1];
$url .= $markers;
}
return "<img class='wp-strava-img' src='{$url}' />";
}
private static function decode_polyline($enc) {
$points = Polyline::decode($enc);
$points = Polyline::pair($points);
$start = $points[0];
$finish = $points[count($points)-1];
/**
* From an encoded polyline, get the start and finish points for
* the purposes of displaying start and finish markers.
*
* @static
* @see https://developers.google.com/maps/documentation/utilities/polylinealgorithm
* @access private
* @param string $enc Encoded polyline.
* @return array with indexes of start & finish containing lat/lon for each.
*/
private static function decode_start_finish( $enc ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Polyline.php';
$points = Polyline::decode( $enc );
$points = Polyline::pair( $points );
$start = $points[0];
$finish = $points[ count( $points ) - 1 ];
return array('start' => $start, 'finish' => $finish);
}
return array(
'start' => $start,
'finish' => $finish,
);
}
}