Add the option to display a marker at the start & finish points of an activity or route

This commit is contained in:
Daniel Lintott
2017-12-16 13:05:16 +00:00
parent bbd2cc9e58
commit abbdf7af95
5 changed files with 180 additions and 5 deletions
+21 -1
View File
@@ -1,5 +1,7 @@
<?php
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Polyline.php';
class WPStrava_StaticMap {
/**
@@ -12,8 +14,9 @@ class WPStrava_StaticMap {
* @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.
*/
public static function get_image_tag( $ride, $height = 320, $width = 480 ) {
public static function get_image_tag( $ride, $height = 320, $width = 480, $markers = false ) {
$key = WPStrava::get_instance()->settings->gmaps_key;
// Short circuit if missing key or ride object doesn't have the data we need.
@@ -27,11 +30,28 @@ class WPStrava_StaticMap {
if ( ! empty( $ride->map->polyline ) && ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) ) {
$url .= $ride->map->polyline;
$points = self::decode_polyline($ride->map->polyline);
} elseif ( ! empty( $ride->map->summary_polyline ) ) {
$url .= $ride->map->summary_polyline;
$points = self::decode_polyline($ride->map->summary_polyline);
}
if ($markers) {
$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];
return array('start' => $start, 'finish' => $finish);
}
}