Initial pass at static Mapbox maps

This commit is contained in:
Justin Foell
2021-11-26 16:36:39 -06:00
parent 6f5cd24ebc
commit e1814ccfb6
7 changed files with 147 additions and 46 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ class WPStrava_ActivityRenderer {
$activity_output .= $activity->get_activity_link( $activity_output .= $activity->get_activity_link(
$activity_details->id, $activity_details->id,
WPStrava_StaticMap::get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'], $activity_details->name ), WPStrava_StaticMap::get_map()->get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'], $activity_details->name ),
$activity_details->name $activity_details->name
); );
+1 -1
View File
@@ -54,7 +54,7 @@ class WPStrava_LatestMap {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Image OK. // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Image OK.
$strava_activity->get_activity_link( $strava_activity->get_activity_link(
$activity->id, $activity->id,
WPStrava_StaticMap::get_image_tag( $activity, null, null, false, $activity->name ), WPStrava_StaticMap::get_map()->get_image_tag( $activity, null, null, false, $activity->name ),
$activity->name $activity->name
); );
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
+1 -1
View File
@@ -60,7 +60,7 @@ class WPStrava_RouteRenderer {
$route_output .= $route->get_route_link( $route_output .= $route->get_route_link(
$route_details->id, $route_details->id,
WPStrava_StaticMap::get_image_tag( $route_details, $map_height, $map_width, $atts['markers'], $route_details->name ), WPStrava_StaticMap::get_map()->get_image_tag( $route_details, $map_height, $map_width, $atts['markers'], $route_details->name ),
$route_details->name $route_details->name
); );
+1 -1
View File
@@ -63,7 +63,7 @@ class WPStrava_SegmentsRenderer {
$segments_output .= $segments->get_segments_link( $segments_output .= $segments->get_segments_link(
$segment_details->id, $segment_details->id,
WPStrava_StaticMap::get_image_tag( $segment_details, $map_height, $map_width, $atts['markers'], $segment_details->name ), WPStrava_StaticMap::get_map()->get_image_tag( $segment_details, $map_height, $map_width, $atts['markers'], $segment_details->name ),
$segment_details->name $segment_details->name
); );
+56
View File
@@ -0,0 +1,56 @@
<?php
class WPStrava_StaticMapGoogle extends WPStrava_StaticMap {
/**
* Get an image tag to a static google map. Will render with
* detailed polyline if not greater than 1865 chars, otherwise
* rendering will use summary polyline.
*
* @static
* @access public
* @param object $activity Activity object to get image tag for.
* @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 string $title Title attribute to accompany image (default empty).
* @return string HTML img tag with static map image.
*/
public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ) {
$key = WPStrava::get_instance()->settings->gmaps_key;
// Short circuit if missing key or activity object doesn't have the data we need.
if ( empty( $key ) || empty( $activity->map ) ) {
return '';
}
if ( ! $height || ! $width ) {
$height = 320;
$width = 480;
}
$url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&scale=2&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:";
$url_len = strlen( $url );
$polyline = '';
if ( ! empty( $activity->map->polyline ) && ( $url_len + strlen( $activity->map->polyline ) < self::$max_chars ) ) {
$polyline = $activity->map->polyline;
} elseif ( ! empty( $activity->map->summary_polyline ) ) {
$polyline = $activity->map->summary_polyline;
} elseif ( ! empty( $activity->map->polyline ) ) {
// Need to reduce the polyline b/c it's too big and no summary was provided.
$polyline = $this->reduce_polyline( $url_len, $activity->map->polyline );
}
$url .= $polyline;
if ( $markers ) {
$points = $this->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;
}
$title_attr = $title ? " title='" . esc_attr( $title ) . "'" : '';
return "<img class='wp-strava-img' src='{$url}'{$title_attr} />";
}
}
+28 -41
View File
@@ -1,8 +1,8 @@
<?php <?php
class WPStrava_StaticMap { abstract class WPStrava_StaticMap {
private static $max_chars = 1865; protected static $max_chars = 1865;
/** /**
* Get an image tag to a static google map. Will render with * Get an image tag to a static google map. Will render with
@@ -18,42 +18,25 @@ class WPStrava_StaticMap {
* @param string $title Title attribute to accompany image (default empty). * @param string $title Title attribute to accompany image (default empty).
* @return string HTML img tag with static map image. * @return string HTML img tag with static map image.
*/ */
public static function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ) { abstract public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' );
$key = WPStrava::get_instance()->settings->gmaps_key;
// Short circuit if missing key or activity object doesn't have the data we need. /**
if ( empty( $key ) || empty( $activity->map ) ) { * Factory method to get the correct StaticMap class based on specified string
return ''; * or by the options setting.
*
* @param string $auth 'google' or 'mapbox' (default 'google').
* @return WPStrava_StaticMap Instance of StaticMap
* @author Justin Foell <justin@foell.org>
* @since NEXT
*/
public static function get_map( $type = 'google' ) {
/*
if ( 'google' === $type ) {
return new WPStrava_StaticGMap();
} }
*/
if ( ! $height || ! $width ) { // Default to refresh.
$height = 320; return new WPStrava_StaticMapbox();
$width = 480;
}
$url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&scale=2&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:";
$url_len = strlen( $url );
$polyline = '';
if ( ! empty( $activity->map->polyline ) && ( $url_len + strlen( $activity->map->polyline ) < self::$max_chars ) ) {
$polyline = $activity->map->polyline;
} elseif ( ! empty( $activity->map->summary_polyline ) ) {
$polyline = $activity->map->summary_polyline;
} elseif ( ! empty( $activity->map->polyline ) ) {
// Need to reduce the polyline b/c it's too big and no summary was provided.
$polyline = self::reduce_polyline( $url_len, $activity->map->polyline );
}
$url .= $polyline;
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;
}
$title_attr = $title ? " title='" . esc_attr( $title ) . "'" : '';
return "<img class='wp-strava-img' src='{$url}'{$title_attr} />";
} }
/** /**
@@ -76,7 +59,7 @@ class WPStrava_StaticMap {
* } * }
* } * }
*/ */
private static function decode_start_finish( $enc ) { protected function decode_start_finish( $enc ) {
require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php';
$points = Polyline::decode( $enc ); $points = Polyline::decode( $enc );
$points = Polyline::pair( $points ); $points = Polyline::pair( $points );
@@ -91,13 +74,13 @@ class WPStrava_StaticMap {
* From a (large) encoded polyline, reduce the number of points * From a (large) encoded polyline, reduce the number of points
* until it is small enough for a GET URL. * until it is small enough for a GET URL.
* *
* @param int $url_len Length of map URL. * @param int $base_url_len Length of map URL.
* @param string $enc Encoded polyline. * @param string $enc Encoded polyline.
* @return string Smaller encoded polyline. * @return string Smaller encoded polyline.
* @author Justin Foell <justin@foell.org> * @author Justin Foell <justin@foell.org>
* @since 2.10.0 * @since 2.10.0
*/ */
private static function reduce_polyline( $url_len, $enc ) { protected function reduce_polyline( $base_url_len, $enc ) {
require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php';
$points = Polyline::decode( $enc ); $points = Polyline::decode( $enc );
$points = Polyline::pair( $points ); $points = Polyline::pair( $points );
@@ -109,11 +92,15 @@ class WPStrava_StaticMap {
$points = Polyline::flatten( $points ); $points = Polyline::flatten( $points );
$polyline = Polyline::encode( $points ); $polyline = Polyline::encode( $points );
if ( $url_len + strlen( $polyline ) >= self::$max_chars ) { if ( $base_url_len + $this->polyline_length( $polyline ) >= self::$max_chars ) {
// Reduce again. // Reduce again.
$polyline = self::reduce_polyline( $url_len, $polyline ); $polyline = $this->reduce_polyline( $base_url_len, $polyline );
} }
return $polyline; return $polyline;
} }
protected function polyline_length( $polyline ) {
return strlen( $polyline );
}
} }
+58
View File
@@ -0,0 +1,58 @@
<?php
class WPStrava_StaticMapbox extends WPStrava_StaticMap {
public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ) {
$polyline = '';
if ( ! empty( $activity->map->polyline ) ) {
$polyline = $activity->map->polyline;
} elseif ( ! empty( $activity->map->summary_polyline ) ) {
$polyline = $activity->map->summary_polyline;
}
if ( empty( $polyline ) ) {
// No polyline provided.
return '';
}
$url = $this->build_url( $polyline, $height, $width, $markers );
$url_len = strlen( $url );
if ( $url_len > self::$max_chars ) {
// Need to reduce the polyline b/c it's too big.
$polyline = $this->reduce_polyline( $url_len - $this->polyline_length( $polyline ), $polyline );
$url = $this->build_url( $polyline, $height, $width, $markers );
}
$title_attr = $title ? " title='" . esc_attr( $title ) . "'" : '';
return "<img class='wp-strava-img' src='{$url}'{$title_attr} />";
}
private function build_url( $polyline, $height = 320, $width = 480, $markers = false ) {
$url = 'https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/static/';
$size = "auto/{$width}x{$height}@2x";
$token = 'access_token=pk.eyJ1IjoianJmb2VsbCIsImEiOiJ4NkNwU2RjIn0.MHjY7k0Okawa3bdV9HtSXg';
$path = array();
if ( $markers ) {
$points = $this->decode_start_finish( $polyline );
$path[] = "pin-s+008000({$points['start'][0]},{$points['start'][1]})";
$path[] = "pin-s+ff0000({$points['finish'][0]},{$points['finish'][1]})";
}
// polyline must be URL encoded https://stackoverflow.com/a/65523379/2146022
$url_polyline = rawurlencode( $polyline );
$path[] = "path-2+ff0000({$url_polyline})";
$url .= implode( ',', $path ) . "/{$size}?{$token}";
return $url;
}
protected function polyline_length( $polyline ) {
return strlen( rawurlencode( $polyline ) );
}
}