Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfreneda/d632091d71a7d26ff2a379657f979066 to your computer and use it in GitHub Desktop.
Save lfreneda/d632091d71a7d26ff2a379657f979066 to your computer and use it in GitHub Desktop.
google maps json style to google maps static url 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('&');
}
var styles = [{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [{
"color": "#444444"
}]
}, {
"featureType": "landscape",
"elementType": "all",
"stylers": [{
"color": "#f2f2f2"
}]
}, {
"featureType": "poi",
"elementType": "all",
"stylers": [{
"visibility": "on"
}]
}, {
"featureType": "road",
"elementType": "all",
"stylers": [{
"saturation": -100
}, {
"lightness": 45
}]
}, {
"featureType": "road.highway",
"elementType": "all",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "transit",
"elementType": "all",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "water",
"elementType": "all",
"stylers": [{
"color": "#BBDEFB"
}, {
"visibility": "on"
}]
}];
var styleAsUrl = get_static_style(styles);
console.log(styleAsUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment