android 计入高德地图——实时天气和天气预报
16lz
2021-01-26
看下实现出来的效果:
必要条件
1.高德地图搜索功能的jar包
2.导入android studio
3.开始接入项目 (重点接入的详细步骤)
天气查询的2个请求参数类为WEATHER_TYPE_LIVE为实况天气;WEATHER_TYPE_FORECAST为预报天气,默认为 实况气。
package com.mondulecircle.ui.activity.home.home_weather;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.widget.TextView;import com.amap.api.services.weather.LocalDayWeatherForecast;import com.amap.api.services.weather.LocalWeatherForecastResult;import com.amap.api.services.weather.LocalWeatherLive;import com.amap.api.services.weather.LocalWeatherLiveResult;import com.amap.api.services.weather.WeatherSearch;import com.amap.api.services.weather.WeatherSearchQuery;import com.modulebase.base.BaseActivity;import com.mondulecircle.R;import com.mondulecircle.ui.adapter.home.HomeWeatherAdapter;import com.mondulecircle.utils.HideStatusBar;import java.util.ArrayList;import java.util.List;/** * 时间:2018/12/22. * 描述:天气预报 */public class WeatherActivity extends BaseActivity implements WeatherSearch.OnWeatherSearchListener { private WeatherSearchQuery mquery; private WeatherSearch mweathersearch; private LocalWeatherLive weatherlive; private static final int CODE = 1000; private TextView tvCity; private TextView tvWeatherCondition; private TextView tvMinimumDegrees; private TextView tvWind; private TextView tvHumidity; private TextView tvUpdate; private RecyclerView rcvWeatherList; private HomeWeatherAdapter homeWeatherAdapter; private Listdata; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); HideStatusBar.fullScreen(WeatherActivity.this); setContentView(R.layout.activity_weather_layout); initView(); } //初始化 private void initView() { String weather = getIntent().getStringExtra("city"); tvCity = (TextView) findViewById(R.id.tv_city); rcvWeatherList = (RecyclerView) findViewById(R.id.rcv_weather_list); tvWeatherCondition = (TextView) findViewById(R.id.tv_weather_condition); tvMinimumDegrees = (TextView) findViewById(R.id.tv_minimum_degrees); tvWind = (TextView) findViewById(R.id.tv_wind); tvHumidity = (TextView) findViewById(R.id.tv_humidity); tvUpdate = (TextView) findViewById(R.id.tv_update); //实时 mquery = new WeatherSearchQuery(weather, WeatherSearchQuery.WEATHER_TYPE_LIVE); mweathersearch = new WeatherSearch(this); mweathersearch.setOnWeatherSearchListener(this); mweathersearch.setQuery(mquery); //异步搜索 mweathersearch.searchWeatherAsyn(); //预报未来几天 mquery = new WeatherSearchQuery(weather, WeatherSearchQuery.WEATHER_TYPE_FORECAST); mweathersearch = new WeatherSearch(this); mweathersearch.setOnWeatherSearchListener(this); mweathersearch.setQuery(mquery); //异步搜索 mweathersearch.searchWeatherAsyn(); data=new ArrayList<>(); homeWeatherAdapter = new HomeWeatherAdapter(WeatherActivity.this, data); rcvWeatherList.setAdapter(homeWeatherAdapter); LinearLayoutManager layout = new LinearLayoutManager(WeatherActivity.this, LinearLayoutManager.VERTICAL, false); rcvWeatherList.setLayoutManager(layout); } /** * 实时天气查询回调 */ @Override public void onWeatherLiveSearched(LocalWeatherLiveResult weatherLiveResult, int rCode) { if (rCode == CODE) { if (weatherLiveResult != null && weatherLiveResult.getLiveResult() != null) { weatherlive = weatherLiveResult.getLiveResult(); tvCity.setText(weatherlive.getCity()); tvWeatherCondition.setText(weatherlive.getWeather()); //气温 tvMinimumDegrees.setText(weatherlive.getTemperature() + "°"); tvWind.setText(weatherlive.getWindDirection() + "风" + weatherlive.getWindPower() + "级"); tvHumidity.setText("湿度 " + weatherlive.getHumidity() + "%"); tvUpdate.setText("更新于:" + weatherlive.getReportTime()); } else { Log.d("实时天气", "onWeatherLiveSearched: ------------------实时天气------------------" + weatherLiveResult); } } } /** * 天气预报 * @param localWeatherForecastResult * @param i */ @Override public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) { //这个地方需要注意下 是取的getWeatherForecast() List local = localWeatherForecastResult.getForecastResult().getWeatherForecast(); this.data=local; homeWeatherAdapter.update(data); }}
package com.mondulecircle.ui.adapter.home;import android.support.annotation.Nullable;import com.amap.api.services.weather.LocalDayWeatherForecast;import com.chad.library.adapter.base.BaseQuickAdapter;import com.chad.library.adapter.base.BaseViewHolder;import com.mondulecircle.R;import java.util.List;/** * 时间:2018/12/23. * 描述:天气预报 */public class HomeWeatherAdapter extends BaseQuickAdapter { public HomeWeatherAdapter(@Nullable List data) { super(R.layout.item_home_weather_layout, data); this.mData = data; } @Override protected void convert(BaseViewHolder helper, LocalDayWeatherForecast item) { } @Override public void onBindViewHolder(BaseViewHolder holder, int position) { final LocalDayWeatherForecast localWeatherForecast = mData.get(position); //日期 holder.setText(R.id.tv_weather, localWeatherForecast.getDate()); if (localWeatherForecast.getWeek().equals("7")) { holder.setText(R.id.tv_week, "星期日"); } else if (localWeatherForecast.getWeek().equals("1")) { holder.setText(R.id.tv_week, "星期一"); } else if (localWeatherForecast.getWeek().equals("2")) { holder.setText(R.id.tv_week, "星期二"); } else if (localWeatherForecast.getWeek().equals("3")) { holder.setText(R.id.tv_week, "星期三"); } else if (localWeatherForecast.getWeek().equals("4")) { holder.setText(R.id.tv_week, "星期四"); } else if (localWeatherForecast.getWeek().equals("5")) { holder.setText(R.id.tv_week, "星期五"); } else if (localWeatherForecast.getWeek().equals("6")) { holder.setText(R.id.tv_week, "星期六"); } //最低气温 holder.setText(R.id.tv_minimum, localWeatherForecast.getDayTemp()); //最高气温 holder.setText(R.id.tv_maximum_temperature, localWeatherForecast.getNightTemp() + "。"); //天气转态 holder.setText(R.id.tv_weather_condition, localWeatherForecast.getNightWeather()); } //更新数据 public void update(List data) { this.mData = data; notifyDataSetChanged(); }}
功能比较简单,就不一个一个介绍了,值得注意的地方都有备注,相信大家一看就会明白,希望对你有帮助!
更多相关文章
- Android实用闹钟开发(三)---AlarmManager和Calendar
- Android(安卓)获取天气http://write.blog.csdn.net/category
- Android写一个实时输入框功能
- Android获取摄像头视频帧并实时处理(转载)
- android JSON解析数据 android解析天气预报
- android JSON解析数据-解析天气预报
- android 实时采播录音播放或发送出去
- 【Android笔记】(2)ButtonClickPic
- Android(安卓)Internet应用实现获取天气预报的示例代码