Step1 在AndroidManifest.xml設定權限
(1) 加入允許使用網路權限。
<uses-permission android:name="android.permission.INTERNET"/>
(2) 加入允許使用GPS權限。
<uses-permission android:name="android.permission.ACCESS_FIND_LOCATION"/>
(3)加入允許使用網路定位權限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GPSdata"
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-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FIND_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
Step2 加入顯示的欄位,使定位結果更容易檢視。
(1)在string.xml設定介面上的變數及參數。
(2)設定在main.xml的介面排版。
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GPSdata</string>
<string name="longtitude_label">經度</string>
<string name="latitude_label">緯度</string>
</resources>
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/longtitude_label"
/>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/latitude_label"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
Step3 實作LocationListener介面
目的為當座標改變時,我們希望系統能夠自動抓取新的位置,此時必須用LocationListener來監聽位置的變化。
(1)開啟主程式java檔,在Activity後面加上implements LocationListener實作介面,並依照eclips協助下建立初步架構。
GPSdata.java
package com.sam.test;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
publicclass GPSdata extends Activity implements LocationListener{
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/*當位置改變的時候*/
@Override
publicvoid onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
/*當GPS或是網路定位功能關閉時*/
@Override
publicvoid onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
/*當GPS或是網路定位功能開啟時*/
@Override
publicvoid onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
/*定位狀態改變時*/
@Override
publicvoid onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
(2)使用LocationManager取得位置
1. 利用LocationManager幫使用者定位(無論是GPS或是使用AGPS之網路定位)。
2. 必須事先確定使用者是否有開啟定位服務。
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*取得系統定位服務*/
LocationManager status =
(LocationManager)(this.getSystemService(Context.LOCATION_SERVICE));
/*確認是否開啟GPS服務or網路定位服務*/
if(status.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
status.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
locationServiceInitial(); //呼叫locationServiceInitial更新位置
}else{
Toast.makeText(this, "請開啟定位服務", Toast.LENGTH_LONG).show();
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
//開啟設定畫面
}
}
p.s Criteria物件是用來讓我們設定資訊提供者選取的標準。我們可以透過它設定精準度、電力、高度、速度、方位和金錢成本等因素作為選擇提供者的考量
private LocationManager lms;
private String bestProvider = LocationManager.GPS_PROVIDER;
privatevoid locationServiceInitial() {
// TODO Auto-generated method stub
lms = (LocationManager) getSystemService(LOCATION_SERVICE); //取得系統定位服務
Criteria criteria = new Criteria(); //資訊提供者選取標準
bestProvider = lms.getBestProvider(criteria, true);
Location location = lms.getLastKnownLocation(bestProvider); //使用GPS定位座標
getLocation(location);
}
privatevoid getLocation(Location location) {
// TODO Auto-generated method stub
if(location != null){
TextView longitude_txt = (TextView)findViewById(R.id.longitude);
TextView latitude_txt = (TextView)findViewById(R.id.latitude);
Double longitude = location.getLongitude(); //經度get
Double latitude = location.getLatitude(); //緯度get
longitude_txt.setText(String.valueOf(longitude));
latitude_txt.setText(String.valueOf(latitude));
}else{
Toast.makeText(this, "無法定位座標", Toast.LENGTH_LONG).show();
}
}
Step4 更新地點之前必須先設一個boolean getService判斷定位服務是否開啟,當使用者並沒有開啟定位服務時系統才不會出錯。
Step5 接著,在onResume()開啟更新,並設定更新的頻率。當系統進入onPause()時,則停止更新。
@Override
protectedvoid onResume() {
// TODO Auto-generated method stub
super.onResume();
if(getService) {
lms.requestLocationUpdates(bestProvider, 1000, 1, this);
//服務提供者、更新頻率毫秒、最短距離、地點改變時呼叫物件
}
}
@Override
protectedvoid onPause() {
// TODO Auto-generated method stub
super.onPause();
if(getService) {
lms.removeUpdates(this); //離開頁面時停止更新
}
}
}