I've built a tool that allows folks to apply a JSON map style to a Google Map, and am now wanting to add support for the Google Static Maps API.
Using the following styles array:
"[{"stylers":[]},{"featureType":"water","stylers":[{"color":"#d2dce6"}]},{"featureType":"administrative.country","elementType":"geometry","stylers":[{"weight":1},{"color":"#d5858f"}]},{"featureType":"administrative.country","elementType":"labels.text.fill","stylers":[{"color":"#555555"}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"visibility":"off"}]},{"featureType":"administrative.country","stylers":[{"visibility":"on"}]},{"featureType":"road.highway","stylers":[{"saturation":-100},{"lightness":40},{"visibility":"simplified"}]},{"featureType":"road.arterial","stylers":[{"saturation":-100},{"lightness":40},{"visibility":"simplified"}]},{"featureType":"road.local","stylers":[{"saturation":-100},{"visibility":"simplified"}]},{"featureType":"landscape","elementType":"all","stylers":[{"hue":"#FFFFFF"},{"saturation":-100},{"lightness":100}]},{"featureType":"landscape.natural","elementType":"geometry","stylers":[{"saturation":-100}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"visibility":"simplified"},{"saturation":-100}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"saturation":-100},{"lightness":60}]},{"featureType":"poi","elementType":"geometry","stylers":[{"hue":"#FFFFFF"},{"saturation":-100},{"lightness":100},{"visibility":"off"}]}]"
(More documentation on format)
I need to ultimately convert this into a URLescaped string, in the format: style=feature:featureArgument|element:elementArgument|rule1:rule1Argument|rule2:rule2Argument
(More documentation)
So far, I've written this JavaScript to try and convert things, but it isn't working properly:
function get_static_style(styles) {
var result = '';
styles.forEach(function(v, i, a){
if (v.stylers.length > 0) { // Needs to have a style rule to be valid.
result += (v.hasOwnProperty('featureType') ? 'feature:' + v.featureType : 'feature:all') + '|';
result += (v.hasOwnProperty('elementType') ? 'element:' + v.elementType : 'element:all') + '|';
v.stylers.forEach(function(val, i, a){
var propertyname = Object.keys(val)[0];
var propertyval = new String(val[propertyname]).replace('#', '0x');
result += propertyname + ':' + propertyval + '|';
});
}
});
console.log(result);
return encodeURIComponent(result);
}
Alas, this code is outputting the following:
(Right-click and select "copy URL" to see the full path I'm using -- the above is direct from the static images API)
...When instead it should look like this:
Any ideas? Thanks!