Android高德之旅(15)天气查询

作者: 大公爵 | 来源:发表于2017-11-10 15:40 被阅读158次

    废话

    说到吃饭...呸,说到天气,其实这个并不属于地图或者导航的范畴,为什么高德要提供天气查询呢?两个字:讲究。天气查询可以用来改善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展示出来。


    总结

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

    相关文章

      网友评论

        本文标题:Android高德之旅(15)天气查询

        本文链接:https://www.haomeiwen.com/subject/favbmxtx.html