Google maps api 3 数据层:动态样式

Google maps api 3 数据层:动态样式,google-maps-api-3,styling,Google Maps Api 3,Styling,我正在使用下面的函数在地图上设置多段线的样式: countyLayer.addListener('click', function(event) { countyLayer.overrideStyle(event.feature, {strokeWeight: 8}); }); 现在我想在单击多段线时删除样式,前提是之前已设置样式 我试过这个: countyLayer.addListener('click', function(event) { if (feature.strok

我正在使用下面的函数在地图上设置多段线的样式:

countyLayer.addListener('click', function(event) {

  countyLayer.overrideStyle(event.feature, {strokeWeight: 8});

});
现在我想在单击多段线时删除样式,前提是之前已设置样式

我试过这个:

countyLayer.addListener('click', function(event) {

  if (feature.strokeWeight == 8){  //problem is here I think ?

     countyLayer.overrideStyle(event.feature, {strokeWeight: 2});
  }
   else {
     countyLayer.overrideStyle(event.feature, {strokeWeight: 8});
   }
 });
我试图在这里获得一些帮助: 但他没有解释如何在点击时删除样式

感谢您的帮助

一个选项(单击功能时切换中的样式):

更改了此功能:

// When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener('click', function(event) {
  event.feature.setProperty('isColorful', true);
});
致:

工作代码段:

var映射;
函数初始化(){
map=new google.maps.map(document.getElementById('map-canvas'){
缩放:4,
中心:{
拉丁语:-28,
液化天然气:137.883
}
});
//加载GeoJSON。
//map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
map.data.addGeoJson(googleJson);
//将每个字母涂成灰色。当isColorful属性
//设置为true。
map.data.setStyle(函数(特性){
变量颜色='灰色';
if(feature.getProperty('isColorful')){
color=feature.getProperty('color');
}
return/**@type{google.maps.Data.StyleOptions}*/({
fillColor:color,
strokeColor:颜色,
冲程重量:2
});
});
//当用户单击时,设置“isColorful”,更改字母的颜色。
map.data.addListener('click',函数(事件){
event.feature.setProperty('isColorful',!event.feature.getProperty('isColorful');
});
//当用户悬停时,通过列出字母的轮廓来引诱他们单击。
//调用revertStyle()以删除所有重写。这将使用样式规则
//在传递给setStyle()的函数中定义
map.data.addListener('mouseover',函数(事件){
map.data.revertStyle();
map.data.overrideStyle(event.feature{
冲程重量:8
});
});
map.data.addListener('mouseout',函数(事件){
map.data.revertStyle();
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
var googleJson={
“类型”:“FeatureCollection”,
“特点”:[{
“类型”:“功能”,
“财产”:{
“字母”:“G”,
“颜色”:“蓝色”,
“等级”:“7”,
“ascii”:“71”
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[123.61, -22.14],
[122.38, -21.73],
[121.06, -21.69],
[119.66, -22.22],
[119.00, -23.40],
[118.65, -24.76],
[118.43, -26.07],
[118.78, -27.56],
[119.22, -28.57],
[120.23, -29.49],
[121.77, -29.87],
[123.57, -29.64],
[124.45, -29.03],
[124.71, -27.95],
[124.80, -26.70],
[124.80, -25.60],
[123.61, -25.64],
[122.56, -25.64],
[121.72, -25.72],
[121.81, -26.62],
[121.86, -26.98],
[122.60, -26.90],
[123.57, -27.05],
[123.57, -27.68],
[123.35, -28.18],
[122.51, -28.38],
[121.77, -28.26],
[121.02, -27.91],
[120.49, -27.21],
[120.14, -26.50],
[120.10, -25.64],
[120.27, -24.52],
[120.67, -23.68],
[121.72, -23.32],
[122.43, -23.48],
[123.04, -24.04],
[124.54, -24.28],
[124.58, -23.20],
[123.61, -22.14]
]
]
}
}, {
“类型”:“功能”,
“财产”:{
“信”:“o”,
“颜色”:“红色”,
“等级”:“15”,
“ascii”:“111”
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[128.84, -25.76],
[128.18, -25.60],
[127.96, -25.52],
[127.88, -25.52],
[127.70, -25.60],
[127.26, -25.79],
[126.60, -26.11],
[126.16, -26.78],
[126.12, -27.68],
[126.21, -28.42],
[126.69, -29.49],
[127.74, -29.80],
[128.80, -29.72],
[129.41, -29.03],
[129.72, -27.95],
[129.68, -27.21],
[129.33, -26.23],
[128.84, -25.76]
],
[
[128.45, -27.44],
[128.32, -26.94],
[127.70, -26.82],
[127.35, -27.05],
[127.17, -27.80],
[127.57, -28.22],
[128.10, -28.42],
[128.49, -27.80],
[128.45, -27.44]
]
]
}
}, {
“类型”:“功能”,
“财产”:{
“信”:“o”,
“颜色”:“黄色”,
“等级”:“15”,
“ascii”:“111”
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[131.87, -25.76],
[131.35, -26.07],
[130.95, -26.78],
[130.82, -27.64],
[130.86, -28.53],
[131.26, -29.22],
[131.92, -29.76],
[132.45, -29.87],
[133.06, -29.76],
[133.72, -29.34],
[134.07, -28.80],
[134.20, -27.91],
[134.07, -27.21],
[133.81, -26.31],
[133.37, -25.83],
[132.71, -25.64],
[131.87, -25.76]
],
[
[133.15, -27.17],
[132.71, -26.86],
[132.09, -26.90],
[131.74, -27.56],
[131.79, -28.26],
[132.36, -28.45],
[132.93, -28.34],
[133.15, -27.76],
[133.15, -27.17]
]
]
}
}, {
“类型”:“功能”,
“财产”:{
“字母”:“g”,
“颜色”:“蓝色”,
“等级”:“7”,
“ascii”:“103”
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[138.12, -25.04],
[136.84, -25.16],
[135.96, -25.36],
[135.26, -25.99],
[135, -26.90],
[135.04, -27.91],
[135.26, -28.88],
[136.05, -29.45],
[137.02, -29.49],
[137.81, -29.49],
[137.94, -29.99],
[137.90, -31.20],
[137.85, -32.24],
[136.88, -32.69],
[136.45, -32.36],
[136.27, -31.80],
// When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener('click', function(event) {
  event.feature.setProperty('isColorful', !event.feature.getProperty('isColorful'));
});