Android 基于百度的天气预报

我在做一个项目需要一个天气预报这个功能,找了一些资料好不容易整出来,
先上个效果图吧这是主页面

现在就跟大家分享吧,闲话不多说,我就直接上代码吧

资料下载天气预报小demo

WeatherScreen.java

package cn.hhs.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.hhs.util.DataUtil;
import cn.hhs.util.HttpService;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
/**
 * @author 家铄
 * @QQ  1466181491
 *
 */
public class WeatherScreen extends Activity implements OnClickListener {
   
    BMapManager mBMapMan = null;
    LocationListener mLocationListener = null;
    MKSearch mSearch = null;
    String npCityId="";
    EditText dialogCity;
    String provinceName, cityName;
    boolean flag =true;
    ProgressDialog progressDialog;
    LinearLayout ll_yes,ll_no;
    TextView tv_city,tv_today,tv_attr1,tv_attr2,tv_attr3,tv_noresult;
    TextView tv_date1,tv_date2,tv_wd1,tv_wd2;
    ImageView ima,ima1,ima2;
    Button btn_return,btn_other;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.weather_screen);
        initView();
        initBaiDuMap();

    }
    @Override
    protected void onPause() {
        mBMapMan.getLocationManager().removeUpdates(mLocationListener);
        mBMapMan.stop();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
        mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_GPS_PROVIDER);
        mBMapMan.start();
        super.onResume();
    }

    /**
     * 
     * 方法名:initBaiDuMap 
     * 功能:初始化百度地图
     * 参数:
     */
    private void initBaiDuMap(){
        mBMapMan = new BMapManager(getApplication());
        mBMapMan.init("14A97FC2DDF678193F61C19C0A20EA29C49DEF5C", null);//  14A97FC2DDF678193F61C19C0A20EA29C49DEF5C
        mBMapMan.start();
        initMyLocation();
    }


    /**
     * 
     * 方法名:initMyLocation 
     * 功能:启动定位
     * 参数:
     */
    private void initMyLocation(){
        progressDialog = ProgressDialog.show(this,null, "城市定位中...",true, true);
        mLocationListener = new LocationListener(){
            @Override
            public void onLocationChanged(Location location) {
                if(location != null&& flag){
                    progressDialog.dismiss();
                    flag = false;
                    GeoPoint myPt = new GeoPoint((int)(location.getLatitude()*1e6),
                            (int)(location.getLongitude()*1e6));
                    initMapSerach();
                    //将当前坐标转化为地址获取当前城市名称
                    mSearch.reverseGeocode(myPt);
                }else{
                }
            }
        };
    }
    private void initMapSerach(){
          // 初始化搜索模块,注册事件监听
      mSearch = new MKSearch();
      mSearch.init(mBMapMan, new MKSearchListener(){

            public void onGetPoiResult(MKPoiResult res, int type, int error) {

            }
            public void onGetDrivingRouteResult(MKDrivingRouteResult res,
                    int error) {
            }
            public void onGetTransitRouteResult(MKTransitRouteResult res,
                    int error) {
            }
            public void onGetWalkingRouteResult(MKWalkingRouteResult res,
                    int error) {
            }
            public void onGetAddrResult(MKAddrInfo res, int error) {
                if (error != 0 || res == null) {
                }else{
                    String city = res.addressComponents.city;
                    String pro = res.addressComponents.province;
                    if(city!=null){
                        provinceName = pro.substring(0, pro.length()-1);
                        cityName =  city.substring(0, city.length()-1);
                        progressDialog = ProgressDialog.show(WeatherScreen.this,null, "天气查询中...",true, true);
                        QueryAsyncTask asyncTask = new QueryAsyncTask();
                        asyncTask.execute("");
                    }else{
                        Toast.makeText(WeatherScreen.this, "定位不到当前城市,无法查询天气", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {

            }
      });

    }

    /**
     * 
     * 方法名:initView 
     * 功能:初始化控件
     * 参数:
     */
    private void initView(){
         ll_yes = (LinearLayout)this.findViewById(R.id.ws2_ll_yes);
         ll_no= (LinearLayout)this.findViewById(R.id.ws2_ll_no);

         tv_city= (TextView)this.findViewById(R.id.ws2_tv_city);
         ima= (ImageView)this.findViewById(R.id.ws2_iv_image);
         tv_attr1= (TextView)this.findViewById(R.id.ws2_tv_attr1);
         tv_attr2= (TextView)this.findViewById(R.id.ws2_tv_attr2);
         tv_attr3= (TextView)this.findViewById(R.id.ws2_tv_attr3);

         tv_noresult = (TextView)this.findViewById(R.id.ws2_tv_noresult);

         tv_date1= (TextView)this.findViewById(R.id.ws2_tv_1_date);
         tv_date2= (TextView)this.findViewById(R.id.ws2_tv_2_date);
         tv_wd1= (TextView)this.findViewById(R.id.ws2_tv_1_wd);
         tv_wd2
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值