高德地图天气预报

import java.util.List;

import android.content.Context;
import android.widget.Toast;

import com.amap.api.location.AMapLocalDayWeatherForecast;
import com.amap.api.location.AMapLocalWeatherForecast;
import com.amap.api.location.AMapLocalWeatherListener;
import com.amap.api.location.AMapLocalWeatherLive;
import com.amap.api.location.LocationManagerProxy;

public class CurrentWeatherReport implements AMapLocalWeatherListener {
	private LocationManagerProxy mLocationManagerProxy;
	Context mContext;

	public CurrentWeatherReport(Context context) {
		this.mContext = context;
	}

	/**
	 * 注册天气监听
	 * 
	 * @param i
	 *            LocationManagerProxy.WEATHER_TYPE_LIVE(实况天气)
	 *            LocationManagerProxy.WEATHER_TYPE_FORECAST(天气预报)
	 */
	public void init(int i) {
		// WEATHER_TYPE_LIVE(实况天气)WEATHER_TYPE_FORECAST(天气预报)
		// listener为天气监听
		mLocationManagerProxy = LocationManagerProxy.getInstance(mContext);
		mLocationManagerProxy.requestWeatherUpdates(i, this);
	}

	// 注意:如果需要同时获取实时天气、未来天气数据时,请确保获取当前位置后再调用。分开调用时,请忽略本句。
	@Override
	public void onWeatherLiveSearched(AMapLocalWeatherLive aMapLocalWeatherLive) {
		// AMapLocalWeatherLive类为实况天气类,通过该对象可获取当前的气温、风向、风力、空气湿度百分比等天气情况。
		if (aMapLocalWeatherLive != null
				&& aMapLocalWeatherLive.getAMapException().getErrorCode() == 0) {
			String city = aMapLocalWeatherLive.getCity();// 城市
			String weather = aMapLocalWeatherLive.getWeather();// 天气情况
			String windDir = aMapLocalWeatherLive.getWindDir();// 风向
			String windPower = aMapLocalWeatherLive.getWindPower();// 风力
			String humidity = aMapLocalWeatherLive.getHumidity();// 空气湿度
			String reportTime = aMapLocalWeatherLive.getReportTime();// 数据发布时间
			Toast.makeText(
					mContext,
					city + "\n" + weather + "\n" + windDir + "\n" + windPower
							+ "\n" + humidity + "\n" + reportTime,
					Toast.LENGTH_SHORT).show();
		} else {
			// 获取天气预报失败
			Toast.makeText(mContext, "获取天气预报失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onWeatherForecaseSearched(
			AMapLocalWeatherForecast aMapLocalWeatherForecast) {
		// AMapLocalWeatherForecast 为天气预报类,可获取当前时间在内的3天的天气情况。
		if (aMapLocalWeatherForecast != null
				&& aMapLocalWeatherForecast.getAMapException().getErrorCode() == 0) {

			List<AMapLocalDayWeatherForecast> forcasts = aMapLocalWeatherForecast
					.getWeatherForecast();
			for (int i = 0; i < forcasts.size(); i++) {
				AMapLocalDayWeatherForecast forcast = forcasts.get(i);
				switch (i) {
				// 今天天气
				case 0:
					// 城市
					String city = forcast.getCity();
					String today = "今天 ( " + forcast.getDate() + " )";
					String todayWeather = forcast.getDayWeather() + "    "
							+ forcast.getDayTemp() + "/"
							+ forcast.getNightTemp() + "    "
							+ forcast.getDayWindPower();
					Toast.makeText(mContext,
							city + "\n" + today + "\n" + todayWeather,
							Toast.LENGTH_SHORT).show();
					break;
				// 明天天气
				case 1:

					String tomorrow = "明天 ( " + forcast.getDate() + " )";
					String tomorrowWeather = forcast.getDayWeather() + "    "
							+ forcast.getDayTemp() + "/"
							+ forcast.getNightTemp() + "    "
							+ forcast.getDayWindPower();
					Toast.makeText(mContext, tomorrow + "\n" + tomorrowWeather,
							Toast.LENGTH_SHORT).show();
					break;
				// 后天天气
				case 2:

					String aftertomorrow = "后天( " + forcast.getDate() + " )";
					String aftertomorrowWeather = forcast.getDayWeather()
							+ "    " + forcast.getDayTemp() + "/"
							+ forcast.getNightTemp() + "    "
							+ forcast.getDayWindPower();
					Toast.makeText(mContext,
							aftertomorrow + "\n" + aftertomorrowWeather,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		} else {
			// 获取天气预报失败
			Toast.makeText(mContext, "获取天气预报失败", Toast.LENGTH_SHORT).show();
		}
	}

}


转载于:https://my.oschina.net/spoon2014/blog/420984

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值