除了定位&&地图SDK外,这里需要添加搜索的SDK,这里使用的是AMap_Search_V3.3.0_20160616.jar
没有接触过高德地图的同学请参考
加载一张高德地图
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();
}
}
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();
}
}