android開發之GPS定位詳解

qq_33974741發表於2013-09-26

一、LocationManager

LocationMangager,位置管理器。要想操作定位相關裝置,必須先定義個LocationManager。我們可以通過如下程式碼建立LocationManger物件。

LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

二、LocationListener

LocationListener,位置監聽,監聽位置變化,監聽裝置開關與狀態。

private LocationListener locationListener=new LocationListener() {
        
        /**
         * 位置資訊變化時觸發
         */
        public void onLocationChanged(Location location) {
            updateView(location);
            Log.i(TAG, "時間:"+location.getTime()); 
            Log.i(TAG, "經度:"+location.getLongitude()); 
            Log.i(TAG, "緯度:"+location.getLatitude()); 
            Log.i(TAG, "海拔:"+location.getAltitude()); 
        }
        
        /**
         * GPS狀態變化時觸發
         */
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
            //GPS狀態為可見時
            case LocationProvider.AVAILABLE:
                Log.i(TAG, "當前GPS狀態為可見狀態");
                break;
            //GPS狀態為服務區外時
            case LocationProvider.OUT_OF_SERVICE:
                Log.i(TAG, "當前GPS狀態為服務區外狀態");
                break;
            //GPS狀態為暫停服務時
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Log.i(TAG, "當前GPS狀態為暫停服務狀態");
                break;
            }
        }
    
        /**
         * GPS開啟時觸發
         */
        public void onProviderEnabled(String provider) {
            Location location=lm.getLastKnownLocation(provider);
            updateView(location);
        }
    
        /**
         * GPS禁用時觸發
         */
        public void onProviderDisabled(String provider) {
            updateView(null);
        }
    };

三、Location

Location,位置資訊,通過Location可以獲取時間、經緯度、海拔等位置資訊。上面採用locationListener裡面的onLocationChanged()來獲取location,下面講述如何主動獲取location。

Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);   system.out.println("時間:"+location.getTime());   system.out.println("經度:"+location.getLongitude());  

注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式獲取的location的各個引數值都是為0。

四、GpsStatus.Listener

GpsStatus.Listener ,GPS狀態監聽,包括GPS啟動、停止、第一次定位、衛星變化等事件。

//狀態監聽
    GpsStatus.Listener listener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            switch (event) {
            //第一次定位
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                Log.i(TAG, "第一次定位");
                break;
            //衛星狀態改變
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                Log.i(TAG, "衛星狀態改變");
                //獲取當前狀態
                GpsStatus gpsStatus=lm.getGpsStatus(null);
                //獲取衛星顆數的預設最大值
                int maxSatellites = gpsStatus.getMaxSatellites();
                //建立一個迭代器儲存所有衛星 
                Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
                int count = 0;     
                while (iters.hasNext() && count <= maxSatellites) {     
                    GpsSatellite s = iters.next();     
                    count++;     
                }   
                System.out.println("搜尋到:"+count+"顆衛星");
                break;
            //定位啟動
            case GpsStatus.GPS_EVENT_STARTED:
                Log.i(TAG, "定位啟動");
                break;
            //定位結束
            case GpsStatus.GPS_EVENT_STOPPED:
                Log.i(TAG, "定位結束");
                break;
            }
        };
    };
//繫結監聽狀態
lm.addGpsStatusListener(listener);


五、GpsStatus

GpsStatus,GPS狀態資訊,上面在衛星狀態變化時,我們就用到了GpsStatus。

//例項化    
GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 獲取當前狀態    
//獲取預設最大衛星數    
int maxSatellites = gpsStatus.getMaxSatellites();     
//獲取第一次定位時間(啟動到第一次定位)    
int costTime=gpsStatus.getTimeToFirstFix();   
//獲取衛星    
Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();   
//一般再次轉換成Iterator    
Iterator<GpsSatellite> itrator=iterable.iterator();


六、GpsSatellite
GpsSatellite,定位衛星,包含衛星的方位、高度、偽隨機噪聲碼、訊雜比等資訊。

//獲取衛星    
Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();   
//再次轉換成Iterator    
Iterator<GpsSatellite> itrator=iterable.iterator();   
//通過遍歷重新整理為ArrayList    
ArrayList<GpsSatellite> satelliteList=new ArrayList<GpsSatellite>();    
int count=0;   
int maxSatellites=gpsStatus.getMaxSatellites();   
while (itrator.hasNext() && count <= maxSatellites) {     
    GpsSatellite satellite = itrator.next();     
    satelliteList.add(satellite);     
    count++;   
}    
System.out.println("總共搜尋到"+count+"顆衛星");   
//輸出衛星資訊    
for(int i=0;i<satelliteList.size();i++){   
    //衛星的方位角,浮點型資料    
    System.out.println(satelliteList.get(i).getAzimuth());   
    //衛星的高度,浮點型資料    
    System.out.println(satelliteList.get(i).getElevation());   
    //衛星的偽隨機噪聲碼,整形資料    
    System.out.println(satelliteList.get(i).getPrn());   
    //衛星的訊雜比,浮點型資料    
    System.out.println(satelliteList.get(i).getSnr());   
    //衛星是否有年曆表,布林型資料    
    System.out.println(satelliteList.get(i).hasAlmanac());   
    //衛星是否有星曆錶,布林型資料    
    System.out.println(satelliteList.get(i).hasEphemeris());   
    //衛星是否被用於近期的GPS修正計算    
    System.out.println(satelliteList.get(i).hasAlmanac());   
}


為了便於理解,接下來模擬一個案例,如何在程式程式碼中使用GPS獲取位置資訊。

第一步:新建一個Android工程專案,命名為mygps,目錄結構如下

第二步:修改main.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">
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cursorVisible="false"
        android:editable="false"
        android:id="@+id/editText"/>
</LinearLayout>


第三步:實用Adnroid平臺的GPS裝置,需要新增上許可權

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>   
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


第四步:修改核心元件activity,修改內容如下

package jason.gprs;

import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText editText;
	private LocationManager lm;
	private static final String TAG = "GpsActivity";

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		lm.removeUpdates(locationListener);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		editText = (EditText) findViewById(R.id.editText);
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		// 判斷GPS是否正常啟動
		if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Toast.makeText(this, "請開啟GPS導航...", Toast.LENGTH_SHORT).show();
			// 返回開啟GPS導航設定介面
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivityForResult(intent, 0);
			return;
		}

		// 為獲取地理位置資訊時設定查詢條件
		String bestProvider = lm.getBestProvider(getCriteria(), true);
		// 獲取位置資訊
		// 如果不設定查詢要求,getLastKnownLocation方法傳人的引數為LocationManager.GPS_PROVIDER
		Location location = lm.getLastKnownLocation(bestProvider);
		updateView(location);
		// 監聽狀態
		lm.addGpsStatusListener(listener);
		// 繫結監聽,有4個引數
		// 引數1,裝置:有GPS_PROVIDER和NETWORK_PROVIDER兩種
		// 引數2,位置資訊更新週期,單位毫秒
		// 引數3,位置變化最小距離:當位置距離變化超過此值時,將更新位置資訊
		// 引數4,監聽
		// 備註:引數2和3,如果引數3不為0,則以引數3為準;引數3為0,則通過時間來定時更新;兩者為0,則隨時重新整理

		// 1秒更新一次,或最小位移變化超過1米更新一次;
		// 注意:此處更新準確度非常低,推薦在service裡面啟動一個Thread,在run中sleep(10000);然後執行handler.sendMessage(),更新位置
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
	}

	// 位置監聽
	private LocationListener locationListener = new LocationListener() {

		/**
		 * 位置資訊變化時觸發
		 */
		public void onLocationChanged(Location location) {
			updateView(location);
			Log.i(TAG, "時間:" + location.getTime());
			Log.i(TAG, "經度:" + location.getLongitude());
			Log.i(TAG, "緯度:" + location.getLatitude());
			Log.i(TAG, "海拔:" + location.getAltitude());
		}

		/**
		 * GPS狀態變化時觸發
		 */
		public void onStatusChanged(String provider, int status, Bundle extras) {
			switch (status) {
			// GPS狀態為可見時
			case LocationProvider.AVAILABLE:
				Log.i(TAG, "當前GPS狀態為可見狀態");
				break;
			// GPS狀態為服務區外時
			case LocationProvider.OUT_OF_SERVICE:
				Log.i(TAG, "當前GPS狀態為服務區外狀態");
				break;
			// GPS狀態為暫停服務時
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				Log.i(TAG, "當前GPS狀態為暫停服務狀態");
				break;
			}
		}

		/**
		 * GPS開啟時觸發
		 */
		public void onProviderEnabled(String provider) {
			Location location = lm.getLastKnownLocation(provider);
			updateView(location);
		}

		/**
		 * GPS禁用時觸發
		 */
		public void onProviderDisabled(String provider) {
			updateView(null);
		}

	};

	// 狀態監聽
	GpsStatus.Listener listener = new GpsStatus.Listener() {
		public void onGpsStatusChanged(int event) {
			switch (event) {
			// 第一次定位
			case GpsStatus.GPS_EVENT_FIRST_FIX:
				Log.i(TAG, "第一次定位");
				break;
			// 衛星狀態改變
			case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
				Log.i(TAG, "衛星狀態改變");
				// 獲取當前狀態
				GpsStatus gpsStatus = lm.getGpsStatus(null);
				// 獲取衛星顆數的預設最大值
				int maxSatellites = gpsStatus.getMaxSatellites();
				// 建立一個迭代器儲存所有衛星
				Iterator<GpsSatellite> iters = gpsStatus.getSatellites()
						.iterator();
				int count = 0;
				while (iters.hasNext() && count <= maxSatellites) {
					GpsSatellite s = iters.next();
					count++;
				}
				System.out.println("搜尋到:" + count + "顆衛星");
				break;
			// 定位啟動
			case GpsStatus.GPS_EVENT_STARTED:
				Log.i(TAG, "定位啟動");
				break;
			// 定位結束
			case GpsStatus.GPS_EVENT_STOPPED:
				Log.i(TAG, "定位結束");
				break;
			}
		};
	};

	/**
	 * 實時更新文字內容
	 * 
	 * @param location
	 */
	private void updateView(Location location) {
		if (location != null) {
			editText.setText("裝置位置資訊\n\n經度:");
			editText.append(String.valueOf(location.getLongitude()));
			editText.append("\n緯度:");
			editText.append(String.valueOf(location.getLatitude()));
		} else {
			// 清空EditText物件
			editText.getEditableText().clear();
		}
	}

	/**
	 * 返回查詢條件
	 * 
	 * @return
	 */
	private Criteria getCriteria() {
		Criteria criteria = new Criteria();
		// 設定定位精確度 Criteria.ACCURACY_COARSE比較粗略,Criteria.ACCURACY_FINE則比較精細
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 設定是否要求速度
		criteria.setSpeedRequired(false);
		// 設定是否允許運營商收費
		criteria.setCostAllowed(false);
		// 設定是否需要方位資訊
		criteria.setBearingRequired(false);
		// 設定是否需要海拔資訊
		criteria.setAltitudeRequired(false);
		// 設定對電源的需求
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		return criteria;
	}
}


第五步:執行效果如下,嘿嘿,用的小米3的工程機做的測試,米3 釋出會吹噓的搜星速度確實很快:


作者:jason0539

微博:http://weibo.com/2553717707

部落格:http://blog.csdn.net/jason0539(轉載請說明出處)

相關文章