高德定位获取天气
首先需要从高德的官网申请一个key 下载 AMap_Location_v1.4.1_20150917.jar 包 把该包放在所建android 项目的bins文件里面。
在AndroidManifest.xml 进行配置 把下面代码加入application 中
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="83c4f8b7b8f4ef6b80e6d01dfbbc1d29" />
83c4f8b7b8f4ef6b80e6d01dfbbc1d29 是你申请的key
AndroidManifest.xml 中的权限配置代码
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Java 代码
import java.util.List;
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;
import com.example.map.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Weather extends Activity implements AMapLocalWeatherListener {
LocationManagerProxy mLocationManagerProxy;
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
tv = (TextView) findViewById(R.id.tv);
// 初始化定位对象
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
// mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_LIVE,
// this);
mLocationManagerProxy.requestWeatherUpdates(
LocationManagerProxy.WEATHER_TYPE_FORECAST, this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onWeatherForecaseSearched(AMapLocalWeatherForecast arg0) {
// 未来天气
List<AMapLocalDayWeatherForecast> list=arg0.getWeatherForecast();
StringBuffer SB=new StringBuffer();
for(int i=0;i<list.size();i++){
AMapLocalDayWeatherForecast wea=list.get(i);
Log.d("hello", "城市"+wea.getCity());
Log.d("hello", "时间"+wea.getDate());
Log.d("hello", "温度"+wea.getDayTemp());
Log.d("hello", "风力"+wea.getDayWindPower());
SB.append("城市:"+wea.getCity()+ "\n"+"时间:"+wea.getDate()+ "\n"+"温度:"+wea.getDayTemp()+ "\n"+"风力:"+wea.getDayWindPower()).append("\n");
}
tv.setText(SB.toString());
}
@Override
public void onWeatherLiveSearched(AMapLocalWeatherLive arg0) {
// 当天天气
Log.d("hello", "城市" + arg0.getCity());
Log.d("hello", "温度" + arg0.getTemperature());
Log.d("hello", "风力" + arg0.getWindPower());
}
}
public java.lang.String getCity()
返回城市名称。
public java.lang.String getCityCode()
返回城市编码。
public java.lang.String getReportTime()
返回天气预报发布时间。
public java.lang.String getProvince()
返回省份名称
java.lang.String | getDate()
返回预报天气的年月日。
|
java.lang.String | getDayTemp()
返回白天天气温度,单位:摄氏度。
|
java.lang.String | getDayWeather()
返回白天天气现象,如“晴”、“多云”。
|
java.lang.String | getDayWindDir()
返回白天风向。
|
java.lang.String | getDayWindPower()
返回白天风力,单位:级。
|
java.lang.String | getNightTemp()
返回夜间天气温度,单位:摄氏度。
|
java.lang.String | getNightWeather()
返回夜间天气现象,如“晴”、“多云”。
|
java.lang.String | getNightWindDir()
返回夜间风向。
|
java.lang.String | getNightWindPower()
返回夜间风力,单位:级。
|
java.lang.String | getWeek()
返回预报天气的星期。
|