Added mapbox / gmaps setting switcher

This commit is contained in:
Justin Foell
2021-12-30 14:06:30 -06:00
parent e1814ccfb6
commit 51cc4b7abe
6 changed files with 225 additions and 47 deletions
+38 -2
View File
@@ -2,6 +2,18 @@
class WPStrava_StaticMapbox extends WPStrava_StaticMap {
/**
* Get an image tag to a static mapbox map. Will render with
* detailed polyline if not greater than 1865 chars, otherwise
* rendering will use summary polyline.
*
* @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 = '' ) {
$polyline = '';
@@ -17,6 +29,11 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap {
return '';
}
if ( ! $height || ! $width ) {
$height = 320;
$width = 480;
}
$url = $this->build_url( $polyline, $height, $width, $markers );
$url_len = strlen( $url );
@@ -30,11 +47,22 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap {
return "<img class='wp-strava-img' src='{$url}'{$title_attr} />";
}
/**
* Build a Mapbox Static Map URL.
*
* @param string $polyline Polyline string to overlay.
* @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 Image URL.
* @author Justin Foell <justin@foell.org>
* @since next
*/
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';
$token = WPStrava::get_instance()->settings->mapbox_token;
$path = array();
@@ -47,11 +75,19 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap {
// 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}";
$url .= implode( ',', $path ) . "/{$size}?access_token={$token}";
return $url;
}
/**
* Get the length of a polyline after encoding.
*
* @param mixed $polyline Polyline string.
* @return int Encoded polyline string length.
* @author Justin Foell <justin@foell.org>
* @since next
*/
protected function polyline_length( $polyline ) {
return strlen( rawurlencode( $polyline ) );
}