html js获取天气预报,天气预报-天气预报-示例中心-JS API 示例 | 高德地图API

这段代码展示了如何利用高德地图API获取指定地区的实时天气和未来四天的天气预报。首先,创建地图并设置中心点,然后加载天气插件,通过调用getLive方法获取实时天气信息,包括城市、天气、温度、风向等,并显示在InfoWindow中。接着,使用getForecast方法获取未来4天的天气预报,将预报数据更新到页面元素中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

天气预报查询

html,body,#container{

height:100%;

}

.weather{

width:5rem;

display:inline-block;

padding-left:0.5rem;

}

.sharp{

height: 1rem;

width: 1rem;

background-color: white;

transform: rotateZ(45deg);

box-shadow: 2px 2px 3px rgba(114, 124, 245, .5);

position: inherit;

margin-left: 10.5rem;

margin-top: -6px;

}

预报天气

var map = new AMap.Map('container', {

resizeEnable: true,

center: [116.486409,39.921489],

zoom: 12

});

AMap.plugin('AMap.Weather', function() {

var weather = new AMap.Weather();

//查询实时天气信息, 查询的城市到行政级别的城市,如朝阳区、杭州市

weather.getLive('朝阳区', function(err, data) {

if (!err) {

var str = [];

str.push('

实时天气' + '

');

str.push('

城市/区:' + data.city + '

');

str.push('

天气:' + data.weather + '

');

str.push('

温度:' + data.temperature + '℃

');

str.push('

风向:' + data.windDirection + '

');

str.push('

风力:' + data.windPower + ' 级

');

str.push('

空气湿度:' + data.humidity + '

');

str.push('

发布时间:' + data.reportTime + '

');

var marker = new AMap.Marker({map: map, position: map.getCenter()});

var infoWin = new AMap.InfoWindow({

content: '

'+str.join('')+'

isCustom:true,

offset: new AMap.Pixel(0, -37)

});

infoWin.open(map, marker.getPosition());

marker.on('mouseover', function() {

infoWin.open(map, marker.getPosition());

});

}

});

//未来4天天气预报

weather.getForecast('朝阳区', function(err, data) {

if (err) {return;}

var str = [];

for (var i = 0,dayWeather; i < data.forecasts.length; i++) {

dayWeather = data.forecasts[i];

str.push(dayWeather.date+' '+dayWeather.dayWeather+' '+ dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃');

}

document.getElementById('forecast').innerHTML = str.join('
');

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值