Created
November 12, 2016 13:23
-
-
Save lfreneda/d632091d71a7d26ff2a379657f979066 to your computer and use it in GitHub Desktop.
google maps json style to google maps static url parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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