当你只设置例如fillOpacity 将丢弃所有其他样式并使用 API-defaults。
可能的解决方案:在默认样式函数中实现fillOpacity的更改:
map.data.setStyle(function(feature) {
return ({
fillColor: feature.getProperty('color'),
strokeColor: feature.getProperty('color'),
//set the opacity to 1 when zoom<4
//otherwise set it to .1
fillOpacity: map.getZoom()<4?1:.1
});
});
...现在所需的fillOpacity 已应用于初始地图缩放
要根据缩放应用不同的fillOpacity,您只需再次运行setStyle(使用相同的样式函数作为参数)。可以通过map.getStyle()
轻松访问样式功能
google.maps.event.addListener(map,'zoom_changed',function(){
map.data.setStyle(map.data.getStyle());
});
...仅此而已。
但这不是最优的,当你有很多特性时,API 必须在每次缩放更改时迭代所有特性,以及当更改的缩放不会导致修改 fillOpacity 时(例如在代码中从 3 放大到 2)。
更好的解决方案:
使用存储上次缩放的属性,然后您可以决定样式功能是否必须运行。
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 4,
center: {lat: -28, lng: 137.883}
});
//that's the mentioned property
map.set('lastZoom',map.getZoom());
google.maps.event.addListener(map,'zoom_changed',function(){
//do we need to update the style?
if(this.getZoom()<4!==this.get('lastZoom')<4){
map.data.setStyle(map.data.getStyle());
}
//update the property
this.set('lastZoom',this.getZoom());
});
演示:http://jsfiddle.net/doktormolle/b7u37wax/