gpt4 book ai didi

javascript - 如何刷新天气图层?

转载 作者:行者123 更新时间:2023-12-02 05:06:08 24 4
gpt4 key购买 nike

有谁知道是否有办法在 Google Maps javascript API 中刷新天气图层?

为了提供一些背景知识,我们有一个应用程序,它在浏览器中保持打开状态,每隔几分钟就会更新 map 上的一些信息。我们让用户打开 map 上的天气层,但天气只会在创建层时加载一次。一段时间后,它会过时,我们希望它保持最新状态。我已经尝试了从重新创建图层并将 map 设置为空,然后返回到当前 map 的所有方法,但温度永远不会重新加载。

任何建议都会很有帮助。谢谢!

更新:一些代码片段展示了我如何尝试重新加载天气层。

//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..});

最佳答案

我唯一能想到的可能有帮助的是在天气层处于事件状态时运行的时间循环上创建一个新的天气层。

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);
}

可能有一些更好的解决方案,但我认为这可行。

关于javascript - 如何刷新天气图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com