標籤:
寫這篇文章主要有三個目的:
1.使用高德地圖api定位
2.擷取天氣資料
3.編程練手
檔案結構
資訊清單檔資訊說明:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.tonny" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <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" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.amap.api.v2.apikey" android:value="****" /> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".WeatherActivity" android:label="@string/title_activity_weather" > </activity> </application></manifest>
定位代碼:
package org.tonny;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationListener;import com.amap.api.location.LocationManagerProxy;import com.amap.api.location.LocationProviderProxy;import android.app.Activity;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.location.Location;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends Activity implements AMapLocationListener{ LocationManagerProxy mLocationManagerProxy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationManagerProxy = LocationManagerProxy.getInstance(MainActivity.this); /** * 第一個參數,使用定位的類型,混合模型,如網路,gps等 第二個參數,定位周期 第三個參數,移動多少距離的時候生效(只有GPS模式下有效) */ mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 2 * 1000, 15, MainActivity.this); } public void onClick(View v) { Intent intent = new Intent(MainActivity.this, WeatherActivity.class); startActivity(intent); } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override protected void onDestroy() { super.onDestroy(); mLocationManagerProxy.destroy(); } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(AMapLocation location) { if (location != null && location.getAMapException().getErrorCode() == 0) { Log.e("Hello", location.toString()); } }}
相應的布局檔案:
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看天氣" android:onClick="onClick"/></RelativeLayout>
天氣代碼:
package org.tonny;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 android.app.Activity;import android.os.Bundle;import android.util.Log;public class WeatherActivity extends Activity implements AMapLocalWeatherListener{ private LocationManagerProxy mLocationManagerProxy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather); mLocationManagerProxy = LocationManagerProxy.getInstance(WeatherActivity.this); mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_LIVE, WeatherActivity.this); } @Override protected void onDestroy() { } @Override public void onWeatherForecaseSearched(AMapLocalWeatherForecast forecast) { } /* * (non-Javadoc) * * @see * com.amap.api.location.AMapLocalWeatherListener#onWeatherLiveSearched( * com.amap.api.location.AMapLocalWeatherLive) */ @Override public void onWeatherLiveSearched(AMapLocalWeatherLive live) { Log.e("Weather", live.getCityCode()); Log.e("Weather", live.getCity()); Log.e("Weather", live.getTemperature()); Log.e("Weather", live.getWindDir()); }}
相應的布局檔案:
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看天氣" /></RelativeLayout>
Android學習十一:高德地圖使用