3

Does anyone know if there is a way to refresh the Weather Layer in Google Maps javascript API?

To give a little background, we have an application which stays open in the browser and updates some information on the map every few minutes. We let the users open the weather layer on the map, but the weather only loads once, when the layer is created. After a while, it gets outdated and we'd like it to stay current. I've tried everything from recreating the layer and setting the map to null, then back to the current map, but temperatures never reload.

Any suggestions would really help. Thanks!

Update: Some code snippets to show how I tried reloading the weather layer.

//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..});
bug
  • 101
  • 5
  • Can you include the code you are using to attempt an update to the WeatherLayer? I think if you include the code, it may allow others to help you find a solution. – Sean Mickey Jul 10 '12 at 03:56
  • Sean, I've updated the question with some code snippets. Not sure how they'd help, but I really hope they do. Thanks for your reply. – bug Jul 10 '12 at 18:33
  • 1
    This [question and answer](http://stackoverflow.com/a/7939772/1342180) may help you. – Spectre87 Jul 10 '12 at 20:13
  • Thanks, Alex, but unfortunately it didn't help. Weather still doesn't get refreshed. – bug Jul 12 '12 at 13:43
  • Decided to create a test page in case anyone wants to play around: http://jsfiddle.net/EbHu9 – bug Jul 12 '12 at 14:13

1 Answers1

0

Only thing I can think of that might help is to create a new weather layer on a timeloop that runs while weather layer is active.

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

Might be some better solutions out there but I think this could work.

Kodemon
  • 370
  • 2
  • 11
  • 1
    Thanks for a reply, but I've tried this and the weather layer images do not change, even though the layer gets recreated. Funny thing is that the cloud layer seems to update just fine, it's the temperatures that don't change. – bug Aug 17 '12 at 19:36