How to refresh the weather layer?

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!