[Android] correspondence case study notes between the activity of the high moral map to achieve Weather inquiry

[Overview] app realize the weather is normal query function, and also because often more than one activity to acquire data, then think of a class package, to call to get to when needed.

[High] De api address https://lbs.amap.com/api/android-sdk/guide/map-data/weather

[Note] because a little ignorant to see the document, it will catch his next record brain to write code to query

[Thinking] activity sends a request - get the address - the address to obtain weather - callback to the activity

1, to obtain the current city or region

Because the documentation is provided directly location query, so first get to the site, uses GPSUtils class (see Last Blog: https://blog.csdn.net/gzyh_tech/article/details/82491214 ).

mquery = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_LIVE);

2, along with the feeling of high moral document to go, you can smooth access to weather data.

3. The focus is on the activity of communication.

Method page1 interfaces define a static properties:

 //  查询天气回调
    public interface Event {
        public void setWeather(String weather,String city,String tem);
    }
    public static class EventManager {
        private static Event mEvent;

        public static void setEventListener(Event nm) {
            mEvent = nm;
        }

        public static void raiseEvent(String weather,String city,String tem) {
            mEvent.setWeather(weather,city,tem);
        }
    }
    // 查询天气回调


    private Event event = new Event() {
        @Override
        public void setWeather(String weather,String city,String tem) {
            id_address.setText(city);
            Toast.makeText(getActivity(),city+",天气:"+weather+",温度:"+tem,Toast.LENGTH_SHORT).show();
        }
    };

page2 after the call to return to get the data:

weatherFragment.EventManager.raiseEvent(weather,address,temperature);

The main code is as follows:

package menu.bottombar.bottombar.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
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.WeatherSearchQuery;

import menu.bottombar.bottombar.Fragment.homeFragment;
import menu.bottombar.bottombar.Utils.GPSUtils.GPSUtils;

public class WeatherSearch extends com.amap.api.services.weather.WeatherSearch implements com.amap.api.services.weather.WeatherSearch.OnWeatherSearchListener,AMapLocationListener {


    private String city;
    private String address;
    private String weather;
    private String temperature;
    private Context context;
    private LayoutInflater layoutInflater;
    private TextView mTextView;
    private int code;


    /*高德地图定位*/
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private String[] strMsg;
    public WeatherSearch(Context context) {
        super(context);
        this.layoutInflater= LayoutInflater.from(context);
    }

    public void getWeather(){
        WeatherSearchQuery mquery = new WeatherSearchQuery(city+"", WeatherSearchQuery.WEATHER_TYPE_LIVE);
        com.amap.api.services.weather.WeatherSearch mweathersearch=new com.amap.api.services.weather.WeatherSearch(context);
        mweathersearch.setOnWeatherSearchListener(this);
        mweathersearch.setQuery(mquery);
        mweathersearch.searchWeatherAsyn(); //异步搜索
    }
    public void getLocalToWeather(Context context,int sCode){
        code=sCode;
        this.context=context;
        Location();
    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult weatherLiveResult, int rCode) {
        if (rCode == 1000) {
            if (weatherLiveResult != null&&weatherLiveResult.getLiveResult() != null) {
                LocalWeatherLive weatherlive = weatherLiveResult.getLiveResult();
                weather=weatherlive.getWeather();
                temperature=weatherlive.getTemperature();
                Log.i("Weather",weatherlive.getTemperature()+"");
                Log.i("Weather",weatherlive.getWeather()+"");
                /*reporttime1.setText(weatherlive.getReportTime()+"发布");
                weather.setText(weatherlive.getWeather());
                Temperature.setText(weatherlive.getTemperature()+"°");
                wind.setText(weatherlive.getWindDirection()+"风     "+weatherlive.getWindPower()+"级");
                humidity.setText("湿度         "+weatherlive.getHumidity()+"%");*/
                switch (code){
                    case 1:
                        weatherFragment.EventManager.raiseEvent(weather,address,temperature);
                    break;
                    default:
                       break ;
                }
            }else {
               // Log.i("Weather","获取天气失败!");
            }
        }else {
           // Log.i("Weather","获取天气失败!api失败");
        }
    }

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

    }

    Handler mHandler = new Handler() {
        public void dispatchMessage(android.os.Message msg) {
            switch (msg.what) {
                //定位完成
                case GPSUtils.MSG_LOCATION_FINISH:
                    String result = "";
                    try {
                        AMapLocation loc = (AMapLocation) msg.obj;
                        result = GPSUtils.getLocationStr(loc, 1);
                        strMsg = result.split(",");
                        address=strMsg[4];
                        city=strMsg[3];
                        //Log.e("Weather","city:"+city+",address:"+address+"!");
                        //Toast.makeText(context, "定位成功", Toast.LENGTH_SHORT).show();
                        getWeather();
                    } catch (Exception e) {
                        //Log.e("Weather", "定位失败"+e.toString());
                    }
                    break;
                default:
                    //Log.e("Weather","定位失败:"+msg+"");
                    break;
            }
        };

    };

    public void Location() {
        // TODO Auto-generated method stub
        try {
            locationClient = new AMapLocationClient(context);
            locationOption = new AMapLocationClientOption();
            // 设置定位模式为低功耗模式
            locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            // 设置定位监听
            locationClient.setLocationListener((AMapLocationListener) this);
            locationOption.setOnceLocation(true);//设置为单次定位
            locationClient.setLocationOption(locationOption);// 设置定位参数
            // 启动定位
            locationClient.startLocation();
            mHandler.sendEmptyMessage(GPSUtils.MSG_LOCATION_START);
        } catch (Exception e) {
        }
    }

    @Override
    public void onLocationChanged(AMapLocation loc) {
        if (null != loc) {
            Message msg = mHandler.obtainMessage();
            msg.obj = loc;
            msg.what = GPSUtils.MSG_LOCATION_FINISH;
            mHandler.sendMessage(msg);
        }
    }
}

 

Published 44 original articles · won praise 21 · views 30000 +

Guess you like

Origin blog.csdn.net/gzyh_tech/article/details/82713360