Javascript 如何刷新天气层?
有人知道谷歌地图javascript API中是否有刷新天气层的方法吗 为了提供一点背景,我们有一个应用程序,它在浏览器中保持打开状态,每隔几分钟更新地图上的一些信息。我们允许用户打开地图上的天气层,但天气仅在创建该层时加载一次。过了一段时间,它就过时了,我们希望它保持最新。我尝试了一切,从重新创建图层到将贴图设置为null,然后返回到当前贴图,但温度从未重新加载 任何建议都会很有帮助。谢谢 更新: 一些代码片段展示了我如何尝试重新加载天气层Javascript 如何刷新天气层?,javascript,google-maps-api-3,Javascript,Google Maps Api 3,有人知道谷歌地图javascript API中是否有刷新天气层的方法吗 为了提供一点背景,我们有一个应用程序,它在浏览器中保持打开状态,每隔几分钟更新地图上的一些信息。我们允许用户打开地图上的天气层,但天气仅在创建该层时加载一次。过了一段时间,它就过时了,我们希望它保持最新。我尝试了一切,从重新创建图层到将贴图设置为null,然后返回到当前贴图,但温度从未重新加载 任何建议都会很有帮助。谢谢 更新: 一些代码片段展示了我如何尝试重新加载天气层 //Global variable for the
//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..});
我能想到的唯一一件可能有用的事情是在timeloop上创建一个新的天气层,在天气层处于活动状态时运行
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);
}
可能有一些更好的解决方案,但我认为这是可行的。您是否可以包含尝试更新WeatherLayer的代码?我认为如果你包含代码,它可能会让其他人帮助你找到解决方案。肖恩,我已经用一些代码片段更新了这个问题。我不知道他们会如何帮助我,但我真的希望他们会。谢谢你的回复。这可能对你有帮助。谢谢你,亚历克斯,但不幸的是,这没有帮助。天气仍然无法刷新。决定创建一个测试页面,以防有人想玩:谢谢回复,但我已经尝试过了,天气层图像不会改变,即使重新创建了该层。有趣的是,云层似乎更新得很好,温度不变。