讀取GPS定位服務資訊

GPS定位服務

LBS(Location Based Services)是基於地理位置的服務功能,以目前地理位置提供服務,譬如找附近的圖書館、餐館、飯店等。這裏我們將透過GPS定位服務來取得用戶當前位置。
LocationManager 類別可以擷取系統的定位服務,這個服務允許應用程式在一定的時間間隔取得 GPS 地理位置的更新資料:
1) 呼叫 getSystemService(Context.LOCATION_SERVICE) 實作一個 LocationManager 物件。
2) 呼叫 requestLocationUpdates(String, long, float, LocationListener) 可以獲得 GPS 地理位置的更新資料。
第1個參數是 LocationManager.GPS_PROVIDER 或
LocationManager.NETWORK_PROVIDER。
  第4個參數是 LocationListener 介面,當 GPS 位置更新時,可經由此介面取得資料。
3) 不想再獲得 GPS 地理位置的更新資料時,可呼叫 removeUpdates()。
4) 必須要在 AndroidManifest.xml 檔案內定義 <user-permission> 使用權限項目,
  當使用參數是 LocationManager.GPS_PROVIDER 時,<user-permission> 設定成:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>當您是從模擬器測試應用程式時,可利用 DDMS 來設定 GPS 位置,要加上下一列 <user- permission>設定成:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
當使用參數是 LocationManager. NETWORK _PROVIDER時,<user-permission> 設定成:
<uses-permission android:name="android.permission. ACCESS_ COARSE_LOCATION "/>

當 GPS 位置有所改變時,LocationListener 介面用來接收來自 LocationManager 的通知。這個 LocationListener 介面要事先註冊在 requestLocationUpdates(String, long, float, LocationListener)
 1) 當 GPS 位置資訊被更新時,
      會呼叫 onLocationChanged(Location location),
      依 Location類別的參數可以讀出 GPS 位置的詳細資訊。
2) 當 GPS 位置資訊的狀態被更新時,
      會呼叫 onStatusChanged(String provider, int status, Bundle extras) 。
3) 當 Provider 被用戶啟動時,
      會呼叫 onProviderEnabled(String provider)。
4) 當 Provider 被用戶關閉時,
      會呼叫 onProviderDisabled(String provider)。



Location 類別可表現某一特定時間地理位置的相關資訊:
指令
說明
getLatitude()
取得緯度–Latitude
getLongitude()
取得經度–Longitude
getAccuracy()
取得精確度–Accuracy
getAltitude ()
取得標高–Altitude
getTime()
取得時間–Time
getSpeed()
取得速度–Speed
getBearing()
取得方位–Bearing

本範例在一個 LocationManager 物件內設定 LocationListener 介面後,實作LocationListener 介面中的 onLocationChanged(Location location) 方法,當 GPS 位置更新時,採用 Location 類別讀出 GPS 位置相關資訊,取得緯度、經度、精確度、標高、時間、速度和方位,顯示在螢幕上
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="com.example.android.location">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LocationGPS"  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>


lgps.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"  android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView android:id="@+id/TextView08" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="--Location-GPS--"></TextView>
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView05" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView06" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/TextView07" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

LocationGPS.java

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LocationGPS extends Activity implements LocationListener {
           private LocationManager mLocationManager;
           public  Location getLocation() {  
        LocationManager locMan = (LocationManager)getSystemService(LOCATION_SERVICE);  
        Location location = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);    
        if(location==null){ 
              location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);           
        }  
        Log.v("Status","---location:" + location);  
        return location;  
    }       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
       TextView mTextView08 = (TextView)findViewById(R.id.TextView08);          
       mTextView08.setText("Location-NetWork");           
    }
    @Override   
    protected void onResume() {       
       if (mLocationManager != null) {           
                 mLocationManager.requestLocationUpdates(               
                                       LocationManager.GPS_PROVIDER,               
                                       0,   0,   this);       
                 }               
       super.onResume();   
       }
    @Override   
    protected void onPause() {       
       if (mLocationManager != null) {           
                 mLocationManager.removeUpdates(this);       
                 }               
       super.onPause();   
       } 
    public void onLocationChanged(Location location) {       
       TextView mTextView01 = (TextView)findViewById(R.id.TextView01);
       TextView mTextView02 = (TextView)findViewById(R.id.TextView02);
       TextView mTextView03 = (TextView)findViewById(R.id.TextView03);
       TextView mTextView04 = (TextView)findViewById(R.id.TextView04);
       TextView mTextView05 = (TextView)findViewById(R.id.TextView05);
       TextView mTextView06 = (TextView)findViewById(R.id.TextView06);
       TextView mTextView07 = (TextView)findViewById(R.id.TextView07);
       mTextView01.setText("緯度-Latitude:  " + String.valueOf(location.getLatitude()));
       mTextView02.setText("經度-Longitude:  " + String.valueOf(location.getLongitude()));
       mTextView03.setText("精度-Accuracy:  " + String.valueOf(location.getAccuracy()));
       mTextView04.setText("標高-Latitude:  " + String.valueOf(location.getAltitude()));
       mTextView05.setText("時間-Time:  " + String.valueOf(location.getTime()));
       mTextView06.setText("速度-Speed:  " + String.valueOf(location.getSpeed()));
       mTextView07.setText("方位-Bearing:  " + String.valueOf(location.getBearing()));  
       }
    public void onProviderDisabled(String provider) {        }    
    public void onProviderEnabled(String provider) {        } 
    public void onStatusChanged(String provider, int status, Bundle extras) {       
       switch (status) {       
       case LocationProvider.AVAILABLE:           
                 Log.v("Status", "AVAILABLE");           
                 break;       
                 case LocationProvider.OUT_OF_SERVICE:           
                            Log.v("Status", "OUT_OF_SERVICE");           
                            break;       
                 case LocationProvider.TEMPORARILY_UNAVAILABLE:           
                            Log.v("Status", "TEMPORARILY_UNAVAILABLE");           
                            break;       
       }   
    }
}

沒有留言: