Converting Google Maps styles array to Google Static Maps styles string Converting Google Maps styles array to Google Static Maps styles string javascript javascript

Converting Google Maps styles array to Google Static Maps styles string


Each single style must be supplied with a separate style-parameter:

 function get_static_style(styles) {    var result = [];    styles.forEach(function(v, i, a){      var style='';      if (v.stylers.length > 0) { // Needs to have a style rule to be valid.        style += (v.hasOwnProperty('featureType') ? 'feature:' + v.featureType : 'feature:all') + '|';        style += (v.hasOwnProperty('elementType') ? 'element:' + v.elementType : 'element:all') + '|';        v.stylers.forEach(function(val, i, a){          var propertyname = Object.keys(val)[0];          var propertyval = val[propertyname].toString().replace('#', '0x');          style += propertyname + ':' + propertyval + '|';        });      }      result.push('style='+encodeURIComponent(style))    });    return result.join('&');  }

watch the result


The selected answer didn't work for me.
But only because I had a few objects without styler parameters.
I had to modify it like so:

function get_static_style(styles) {    var result = [];    styles.forEach(function(v, i, a){        var style='';        if( v.stylers ) { // only if there is a styler object            if (v.stylers.length > 0) { // Needs to have a style rule to be valid.                style += (v.hasOwnProperty('featureType') ? 'feature:' + v.featureType : 'feature:all') + '|';                style += (v.hasOwnProperty('elementType') ? 'element:' + v.elementType : 'element:all') + '|';                v.stylers.forEach(function(val, i, a){                    var propertyname = Object.keys(val)[0];                    var propertyval = val[propertyname].toString().replace('#', '0x');                    // changed "new String()" based on: http://stackoverflow.com/a/5821991/1121532                    style += propertyname + ':' + propertyval + '|';                });            }        }        result.push('style='+encodeURIComponent(style));    });    return result.join('&');}

see it in action at: http://jsfiddle.net/ZnGpb/1/

p.s: JSHint


This is a PHP method that does the same thing

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);}

src: https://gist.github.com/WouterDS/5942b891cdad4fc90f40