Skip to content

Instantly share code, notes, and snippets.

@wouterds
Created January 27, 2015 14:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wouterds/5942b891cdad4fc90f40 to your computer and use it in GitHub Desktop.
Save wouterds/5942b891cdad4fc90f40 to your computer and use it in GitHub Desktop.
Convert google maps style json to url arguments to use for google maps static images.
<?php
public function mapStylesUrlArgs($mapStyleJson)
{
$params = [];
foreach (json_decode($mapStyleJson, true) as $style) {
$styleString = '';
if (isset($style['stylers']) && count($style['stylers']) > 0) {
$styleString .= (isset($style['featureType']) ? ('feature:' . $style['featureType']) : 'feature:all') . '|';
$styleString .= (isset($style['elementType']) ? ('element:' . $style['elementType']) : 'element:all') . '|';
foreach ($style['stylers'] as $styler) {
$propertyname = array_keys($styler)[0];
$propertyval = str_replace('#', '0x', $styler[$propertyname]);
$styleString .= $propertyname . ':' . $propertyval . '|';
}
}
$styleString = substr($styleString, 0, strlen($styleString) - 1);
$params[] = 'style=' . $styleString;
}
return implode("&", $params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment