2017-10-19 45 views
2

我正在使用geojson數據在Google地圖中使用圖層。谷歌地圖層onclick更改風格多邊形

我現在擁有的是一個有四個不同多邊形的圖層,當你點擊它們時,它們會從灰色變爲綠色。 但是,當我再次點擊它不會變回灰色,這是點擊功能的目的。

map.data.setStyle(function(feature) { 
 
    var color; 
 
    if (feature.getProperty('isColorful')) { 
 
     color = '#009900'; 
 
    }else{ 
 
     color: 'grey'; 
 
    } 
 
    return ({ 
 
     fillColor: color, 
 
     strokeColor: color, 
 
     strokeWeight: 2 
 
    }); 
 
});

在我的情況下,它工作在我的編輯,https://embed.plnkr.co/hi4MtjO8f0PN6rCW70rE/。 這是完整的代碼片段。

我修好了! 我改變幾行的位置,並將其設置爲false或真

map.data.setStyle(function(feature) { 
 
var color; 
 
if (feature.getProperty('isColorful')) { 
 
    feature.setProperty('isColorful', false); 
 
    color = '#009900'; 
 
}else{ 
 
    color: 'grey'; 
 
} 
 
return /** @type {google.maps.Data.StyleOptions} */({ 
 
    fillColor: color, 
 
    strokeColor: color, 
 
    strokeWeight: 2 
 
}); 
 
}); 
 

 
map.data.addListener('click', function(event) { 
 
var name = event.feature.getProperty('name'); 
 
var index = vm.areas.indexOf(name); 
 
    if(index >= 0){ 
 
    vm.areas.splice(index,1); 
 
    event.feature.setProperty('isColorful', false); 
 
    } else{ 
 
    vm.areas.push(name); 
 
    event.feature.setProperty('isColorful', true); 
 
    } 
 
    $scope.$apply(); 
 
});

+0

嗨,你能告訴我們完整的相關代碼嗎?或者甚至更好,用你的代碼完成代碼片段,這樣我們就可以看到它的工作。 –

+0

我會在那一刻做到這一點 – MissesA

回答

0

您應該將else子句中改變isColorful propertyb太否則,你總是假

map.data.setStyle(function(feature) { 
var color; 

    if (feature.getProperty('isColorful')) { 
     feature.setProperty('isColorful', false); 
     color = '#009900'; 
    }else{ 
     feature.setProperty('isColorful', true); 
     color: 'grey'; 
    } 
+0

感謝您的提示! – MissesA

+0

@Audrey好,如果我的答案是正確的,請將其標記爲已接受...看看這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge