AMap.Weather 天气查询服务,提供城市/区县天气预报服务
高德地图为我们提供了天气预报的查询接口,通过该插件,我们可以获取到未来几天的天气预报详情,这对于想优化app与客户之间的联系是提供了极大的方便的。
以下为官网api
由api我们可知,weather插件分别为我们提供了实时天气信息和未来4天的天气预况,通过该方法我们可以获取相应的对象进而获取相应的信息。由示例:
<!doctype html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="chrome=1"><meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"><style type="text/css">body,html{height: 100%;margin: 0px;}#container{height: 50%;margin: 0px;}#result{height: 50%;margin: 0px;}#weather {height: 180px;background-color: #fff;padding-left: 10px;padding-right: 10px;position: absolute;bottom: 20px;font-size: 12px;right: 10px;border-radius: 3px;line-height: 20px;border: 1px solid #ccc;}.weather{width: 60px;padding-left: 8px;display: inline-block;}</style><title>快速入门</title></head><body><div id="container" tabindex="0"></div><div id="tip" style="line-height: 24px"></div><script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=你的api"></script><script type="text/javascript">var geocoder; var map = new AMap.Map('container', {resizeEnable: true,});AMap.service('AMap.Weather', function() {var weather = new AMap.Weather();//查询实时天气信息, 查询的城市到行政级别的城市,如朝阳区、杭州市weather.getLive('珠海市', function(err, data) {if (!err) {var str = [];str.push('<div style="color: #3366FF;">实时天气' + '</div>');str.push('<div>城市/区:' + data.city + '</div>');str.push('<div>天气:' + data.weather + '</div>');str.push('<div>温度:' + data.temperature + '℃</div>');str.push('<div>风向:' + data.windDirection + '</div>');str.push('<div>风力:' + data.windPower + ' 级</div>');str.push('<div>空气湿度:' + data.humidity + '</div>');str.push('<div>发布时间:' + data.reportTime + '</div>');var marker = new AMap.Marker({map: map, position: map.getCenter()});var infoWin = new AMap.InfoWindow({content: str.join(''),offset: new AMap.Pixel(0, -20)});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+' <div class="weather">'+dayWeather.dayWeather+'</div> '+ dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃');}document.getElementById('tip').innerHTML = str.join('<br>');});});</script></body>
</html>