高德地图——查询天气

除了定位&&地图SDK外,这里需要添加搜索的SDK,这里使用的是AMap_Search_V3.3.0_20160616.jar

没有接触过高德地图的同学请参考
加载一张高德地图

1.查询今日天气

package com.pansoft.oilgas.gaodenavigation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.amap.api.maps.MapView;
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;

public class MainActivity extends AppCompatActivity implements
        com.amap.api.services.weather.WeatherSearch.OnWeatherSearchListener{
    final String tag=MainActivity.class.getSimpleName();
    final String cityString="济南";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WeatherSearchQuery weatherQuery = new WeatherSearchQuery(
                cityString,
                WeatherSearchQuery.WEATHER_TYPE_LIVE);
        WeatherSearch weatherSearch = new WeatherSearch(
                MainActivity.this);
        weatherSearch.setQuery(weatherQuery);
        weatherSearch.setOnWeatherSearchListener(MainActivity.this);
        weatherSearch.searchWeatherAsyn();
    }
    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int rCode) {
        if(rCode==1000){
           LocalWeatherLive liveWeather= localWeatherLiveResult.getLiveResult();
            ShowWeatherFragment showFragment=ShowWeatherFragment.newInstance(liveWeather);
            showFragment.show(getFragmentManager(),"xxxx");

        }else{
            Log.e("","查询天气失败");
        }

    }

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

    }
}

展示天气用到的ShowWeatherFragment

不熟悉DialogFragment的同学请参考
AltertDialog在DialogFragment中的使用

如下:

package com.pansoft.oilgas.gaodenavigation;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.weather.LocalWeatherLive;


public class ShowWeatherFragment extends DialogFragment {
    public static final String KEY_MSG = "weather msg";
    private LocalWeatherLive weatherLive;
    public ShowWeatherFragment() {
        // Required empty public constructor
    }

    public static ShowWeatherFragment newInstance(LocalWeatherLive weatherLive) {
        ShowWeatherFragment fragment = new ShowWeatherFragment();
        Bundle args = new Bundle();
        args.putParcelable(KEY_MSG,weatherLive);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            weatherLive =getArguments().getParcelable(KEY_MSG);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

        builder.setTitle("济南天气");
        builder.setMessage(weatherLive.getCity()+"\n"
        +weatherLive.getWeather()+"\n"
                        +weatherLive.getTemperature()+"\n"

        );
        builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
     return builder.create();
    }
}

这里写图片描述

2.查询未来三天天气

package com.pansoft.oilgas.gaodenavigation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.amap.api.maps.MapView;
import com.amap.api.services.weather.LocalWeatherForecast;
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;

public class MainActivity extends AppCompatActivity implements
        com.amap.api.services.weather.WeatherSearch.OnWeatherSearchListener{
    final String tag=MainActivity.class.getSimpleName();
    final String cityString="济南";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WeatherSearchQuery weatherQuery = new WeatherSearchQuery(
                cityString,
                WeatherSearchQuery.WEATHER_TYPE_FORECAST);
        WeatherSearch weatherSearch = new WeatherSearch(
                MainActivity.this);
        weatherSearch.setQuery(weatherQuery);
        weatherSearch.setOnWeatherSearchListener(MainActivity.this);
        weatherSearch.searchWeatherAsyn();
    }
    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int rCode) {

    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int rCode) {
        if(rCode==1000){
            LocalWeatherForecast weatherForecast= localWeatherForecastResult.getForecastResult();
            ShowWeatherFragment showFragment=ShowWeatherFragment.newInstance(weatherForecast);
            showFragment.show(getFragmentManager(),"xxxx");

        }else{
            Log.e("","查询天气失败");
        }
    }
}
package com.pansoft.oilgas.gaodenavigation;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import com.amap.api.services.weather.LocalDayWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecast;

import java.util.List;


public class ShowWeatherFragment extends DialogFragment {
    public static final String KEY_MSG = "weather msg";
    private LocalWeatherForecast weatherForecast;
    public ShowWeatherFragment() {
        // Required empty public constructor
    }

    public static ShowWeatherFragment newInstance(LocalWeatherForecast weatherForecast) {
        ShowWeatherFragment fragment = new ShowWeatherFragment();
        Bundle args = new Bundle();
        args.putParcelable(KEY_MSG,weatherForecast);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            weatherForecast =getArguments().getParcelable(KEY_MSG);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
     List<LocalDayWeatherForecast> dayWeatherList=weatherForecast.getWeatherForecast();

        String forestMsgInString="";

        for(int j=0;j<dayWeatherList.size();j++){
            forestMsgInString+=dayWeatherList.get(j).getDate()+"\n";
            forestMsgInString+=dayWeatherList.get(j).getDayWeather()+"\n";
            forestMsgInString+=dayWeatherList.get(j).getDayTemp()+"\n";
        }




        builder.setTitle("济南天气");
        builder.setMessage(forestMsgInString);
        builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
     return builder.create();
    }
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值