如何刷新天气图层?

3
有没有办法在Google Maps JavaScript API中刷新天气图层?我们有一个应用程序,它在浏览器中保持打开状态,并每隔几分钟更新地图上的一些信息。我们允许用户在地图上打开天气图层,但天气只在创建图层时加载一次。过了一会儿,它就过时了,我们希望它保持最新状态。我尝试了从重新创建图层和将地图设置为null,然后回到当前地图的所有方法,但温度永远不会重新加载。请问有什么建议吗?谢谢!更新:一些代码片段展示了我如何尝试重新加载天气图层。
//Global variable for the weather layer
var weatherLayer = null;

//When user clicks the weather button on the map
weatherLayer = new google.maps.weather.WeatherLayer({..});
weatherLayer.setMap(myMap);

//When I try to refresh the layer
weatherLayer.setMap(null); //This successfully hides it
weatherLayer = new google.maps.weather.WeatherLayer({...});
weatherLayer.setMap(myMap);  //This doesnt' reload the data.

//Tried resetting the options before and after recreating the layer to see if that would help, but it didn't
weatherLayer.setOptions({..new option set..});

你能否提供尝试更新WeatherLayer的代码?如果您提供了代码,可能会让其他人帮助您找到解决方案。 - Sean Mickey
Sean,我已经更新了问题并添加了一些代码片段。不确定它们如何帮助,但我真的希望它们能够有所帮助。感谢您的回复。 - bug
1
这个问题和答案可能会对你有所帮助。 - Spectre87
谢谢,Alex,但不幸的是它没有帮助。天气仍然没有刷新。 - bug
决定创建一个测试页面,以防有人想要玩一下:http://jsfiddle.net/EbHu9 - bug
1个回答

0

我能想到的唯一有帮助的方法是在天气层处于激活状态时,在时间循环上创建一个新的天气层。

var weatherLayer = null;
var timer = null; // This is for the refresh timer

//Add the click hander to the weather button
$(weatherButton).click(function() {
    if(weatherLayer == null) {
        setWeatherLayer();
    } else {
        clearTimeout(timer);
        weatherLayer.setMap(null);
        weatherLayer = null;
    }
});

function setWeatherLayer() {
    //Create a new layer
    weatherLayer = new google.maps.weather.WeatherLayer({
        temperatureUnits: google.maps.weather.TemperatureUnit.FAHRENHEIT
    });
    weatherLayer.setMap(map);
    timer = setTimeout(setWeatherLayer, 5000);
}

可能有更好的解决方案,但我认为这可以工作。

1
谢谢您的回复,但我已经尝试过这个方法了,天气图层的图片并没有改变,即使该图层被重新创建。有趣的是云层似乎更新得很好,而温度却没有改变。 - bug

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接