谷歌地图 - 每n秒更改标记颜色

时间:2016-12-21 17:07:54

标签: javascript google-maps google-maps-api-3

使用jsfiddle here,想要检查正确的方法,将第2个标记{2}的icon fillColor按顺序更改为["#FF0000", "#00FF00", "#0000FF"]之一到["#0000FF", "#00FF00", "#FF0000"]之一作为第二个标记。我应该使用setInterval吗?

1 个答案:

答案 0 :(得分:2)

jsFiddle

这可能就是你的目标

let x = 1;
setInterval(() => {
    let newShade;
    switch (x) {
        case 1:
            newShade = colors[0];
            x = 2;
            break;
        case 2:
            newShade = colors[1];
            x = 3;
            break;
        case 3:
            newShade = colors[2];
            x = 1;
            break;
    }
    let count = 0;
    console.log(`Running fn inside setInterval`);
    for (let marker of markers) {
        console.log(`trying to get icon for ${count+=1} ${marker}`)
        let icon = marker.getIcon();
        icon.strokeColor = newShade;
        icon.fillColor = newShade;
        marker.setIcon(icon);
    }
}, 5000);