我已经构建了一个工具,允许人们将 JSON 地图样式应用于 Google 地图,现在我想添加对Google Static Maps API的支持。
使用以下样式数组:
"[{"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"}]}]"
我需要最终将其转换为 URLescaped 字符串,格式为:(style=feature:featureArgument|element:elementArgument|rule1:rule1Argument|rule2:rule2Argument
更多文档)
到目前为止,我已经编写了这个 JavaScript 来尝试转换,但它不能正常工作:
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);
}
唉,这段代码输出以下内容:
(右键单击并选择“复制 URL”以查看我正在使用的完整路径——以上是直接来自静态图像 API)
...相反,它应该如下所示:
有任何想法吗?谢谢!