首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android高德之旅(15)天气查询废话简介总结

Android高德之旅(15)天气查询废话简介总结

作者头像
大公爵
发布2018-09-05 17:20:00
8670
发布2018-09-05 17:20:00
举报
文章被收录于专栏:葬爱家族葬爱家族

废话

说到吃饭...呸,说到天气,其实这个并不属于地图或者导航的范畴,为什么高德要提供天气查询呢?两个字:讲究。天气查询可以用来改善app体验的功能,如:在跑步类app中加入天气的提醒;或者出去自驾游提前知道要下雨,就可以造作准备,是不是很贴心。

简介

天气查询的请求参数类为 WeatherSearch,city(城市)为必设参数,type(气象类型)为可选,包含有两种类型:WEATHER_TYPE_LIVE为实况天气;WEATHER_TYPE_FORECAST为预报天气,默认为 实况天气。

WeatherSearchQuery query = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_LIVE);
WeatherSearch weathersearch=new WeatherSearch(this);
weathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
        @Override
        public void onWeatherLiveSearched(LocalWeatherLiveResult liveResult, int i) {
            
        }

        @Override
        public void onWeatherForecastSearched(LocalWeatherForecastResult forecastResult, int i) {

        }
    });
weathersearch.setQuery(query);
weathersearch.searchWeatherAsyn();

代码超级简单,关键来看两个回调,第一个回调是实况天气,第二个回调是预报天气,先来看第一个。

@Override
public void onWeatherLiveSearched(LocalWeatherLiveResult result, int i) {
    LocalWeatherLive liveResult = result.getLiveResult();
    liveTimeTxt.setText(liveResult.getReportTime()); //时间
    liveWeatherTxt.setText(liveResult.getWeather()); //气象
    liveTemperatureTxt.setText(liveResult.getTemperature() + "°C"); //温度
    liveWindDirectionTxt.setText(liveResult.getWindDirection()); //风向
    liveWindPowerTxt.setText(liveResult.getWindPower() + "级"); //风力
    liveHumidityTxt.setText(liveResult.getHumidity() + "%"); //湿度
}

通过返回数据,我们可以获得丰富的天气情况,包括气温,气象,风向,风力,湿度,城市等等。

再来看第二个回调,预报天气,这里会返回未来四天的天气,而且这里的数据分为白天和夜晚,是不是很丰富。

@Override
public void onWeatherForecastSearched(LocalWeatherForecastResult result, int i) {
    LocalWeatherForecast forecastResult = result.getForecastResult();
    forecastTimeTxt.setText(forecastResult.getReportTime());
    List<LocalDayWeatherForecast> weatherForecast = forecastResult.getWeatherForecast();

    List<Forecast> forecastList = new ArrayList<>();
    for (LocalDayWeatherForecast forecast : weatherForecast) {
        String dayWeather = forecast.getDayWeather(); //白天天气
        String dayTemp = forecast.getDayTemp(); //白天温度
        String dayWindDirection = forecast.getDayWindDirection(); //白天风向
        String dayWindPower = forecast.getDayWindPower(); //白天风力
        String nightWeather = forecast.getNightWeather(); //夜晚天气
        String nightTemp = forecast.getNightTemp(); //夜晚温度
        String nightWindDirection = forecast.getNightWindDirection(); //夜晚风向
        String nightWindPower = forecast.getNightWindPower(); //夜晚风力

        forecastList.add(new Forecast(dayWeather, dayTemp, dayWindDirection, dayWindPower,
                nightWeather, nightTemp, nightWindDirection, nightWindPower));
    }

    ForecastAdapter adapter = new ForecastAdapter(this, forecastList);
    forecastListView.setAdapter(adapter);
}

我们把数据解析成一个List<Forecast> forecastList,然后用ListView展示出来。

总结

天气就说这么多,本期节目就到这里,感谢大家收看,我们下期再见~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.11.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 废话
  • 简介
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档