【问题标题】:How to refresh the weather layer?如何刷新天气图层?
【发布时间】:2012-07-08 22:49:19
【问题描述】:

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

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

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

更新: 一些代码 sn-ps 显示我如何尝试重新加载天气层。

//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 的代码?我认为如果您包含代码,它可能会允许其他人帮助您找到解决方案。
  • 肖恩,我已经用一些代码 sn-ps 更新了这个问题。不知道他们会如何提供帮助,但我真的希望他们能提供帮助。感谢您的回复。
  • 这个question and answer 可以帮助你。
  • 谢谢,亚历克斯,但不幸的是它没有帮助。天气仍然没有刷新。
  • 决定创建一个测试页面以防有人想玩:jsfiddle.net/EbHu9

标签: javascript google-maps-api-3


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回复,但我已经尝试过了,即使重新创建了图层,天气图层图像也不会改变。有趣的是,云层似乎更新得很好,只是温度没有变化。
猜你喜欢
  • 2018-06-15
  • 2020-09-09
  • 2012-07-06
  • 1970-01-01
  • 2013-07-06
  • 2011-06-22
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多