如何刷新天气图层? [英] How to refresh the weather layer?

查看:98
本文介绍了如何刷新天气图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否有方法刷新Google Maps JavaScript API中的天气图层?为了给出一点背景,我们有一个应用程序保持打开状态在浏览器中每隔几分钟更新地图上的一些信息。我们让用户在地图上打开天气图层,但天气只在图层创建时加载一次。过了一段时间,它变得过时了,我们希望它保持最新状态。我已经尝试了一切,从重新创建图层并将地图设置为null,然后返回到当前地图,但温度从未重新加载。



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



更新:
一些代码片段显示了我是如何重新加载天气图层的。

  //天气层的全局变量
var weatherLayer = null;

//当用户点击地图上的天气按钮
weatherLayer = new google.maps.weather.WeatherLayer({..});
weatherLayer.setMap(myMap);

//当我尝试刷新图层
weatherLayer.setMap(null); //成功隐藏它
weatherLayer = new google.maps.weather.WeatherLayer({...});
weatherLayer.setMap(myMap); //这不会重新加载数据。

//尝试在重新创建图层前后重置选项,以查看是否有帮助,但它没有
weatherLayer.setOptions({.. new option set ..}) ;


解决方案

在天气图层处于活动状态时运行的时间循环上创建新的天气图层。

  var weatherLayer = null; 
var timer = null; //这是刷新计时器

//将天气按钮添加到天气按钮
$(weatherButton).click(function(){
if(weatherLayer == null){
setWeatherLayer();
} else {
clearTimeout(timer);
weatherLayer.setMap(null);
weatherLayer = null;
}
});

函数setWeatherLayer(){
//创建一个新图层
weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits:google.maps.weather .TemperatureUnit.FAHRENHEIT
});
weatherLayer.setMap(map);
timer = setTimeout(setWeatherLayer,5000);
}

可能会有更好的解决方案,但我认为这可行。 p>

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

解决方案

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.

这篇关于如何刷新天气图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆