Android GPS

Step1在AndroidManifest.xml設定權限

(1)加入允許使用網路權限。

<uses-permissionandroid:name="android.permission.INTERNET"/>

(2)加入允許使用GPS權限。

<uses-permissionandroid:name="android.permission.ACCESS_FIND_LOCATION"/>

(3)加入允許使用網路定位權限。

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


AndroidManifest.xml

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

package="com.sam.test"

android:versionCode="1"

android:versionName="1.0">

<uses-sdkandroid:minSdkVersion="8"/>

<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

<activityandroid:name=".GPSdata"

android:label="@string/app_name">

<intent-filter>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

<uses-permissionandroid:name="android.permission.INTERNET"/>

<uses-permissionandroid:name="android.permission.ACCESS_FIND_LOCATION"/>

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

</manifest>


Step2加入顯示的欄位,使定位結果更容易檢視。

(1)string.xml設定介面上的變數及參數。

(2)設定在main.xml的介面排版。

String.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

<stringname="app_name">GPSdata</string>

<stringname="longtitude_label">經度</string>

<stringname="latitude_label">緯度</string>

</resources>

main.xml

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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

packagecom.sam.test;

importandroid.app.Activity;

importandroid.location.Location;

importandroid.location.LocationListener;

importandroid.os.Bundle;

publicclassGPSdataextendsActivityimplementsLocationListener{

/** Called when the activity is first created. */

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

/*當位置改變的時候*/

@Override

publicvoidonLocationChanged(Location arg0) {

//TODOAuto-generated method stub

}

/*GPS或是網路定位功能關閉時*/

@Override

publicvoidonProviderDisabled(String provider) {

//TODOAuto-generated method stub

}

/*GPS或是網路定位功能開啟時*/

@Override

publicvoidonProviderEnabled(String provider) {

//TODOAuto-generated method stub

}

/*定位狀態改變時*/

@Override

publicvoidonStatusChanged(String provider,intstatus, Bundle extras) {

//TODOAuto-generated method stub

}

}

(2)使用LocationManager取得位置

1.利用LocationManager幫使用者定位(無論是GPS或是使用AGPS之網路定位)

2.必須事先確定使用者是否有開啟定位服務。

publicvoidonCreate(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(newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

//開啟設定畫面

}

}

p.sCriteria物件是用來讓我們設定資訊提供者選取的標準。我們可以透過它設定精準度、電力、高度、速度、方位和金錢成本等因素作為選擇提供者的考量

privateLocationManagerlms;

privateStringbestProvider= LocationManager.GPS_PROVIDER;

privatevoidlocationServiceInitial() {

//TODOAuto-generated method stub

lms= (LocationManager) getSystemService(LOCATION_SERVICE);//取得系統定位服務

Criteria criteria =newCriteria();//資訊提供者選取標準

bestProvider=lms.getBestProvider(criteria,true);

Location location =lms.getLastKnownLocation(bestProvider);//使用GPS定位座標

getLocation(location);

}

privatevoidgetLocation(Location location) {

//TODOAuto-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

protectedvoidonResume() {

//TODOAuto-generated method stub

super.onResume();

if(getService) {

lms.requestLocationUpdates(bestProvider, 1000, 1,this);

//服務提供者、更新頻率毫秒、最短距離、地點改變時呼叫物件

}

}

@Override

protectedvoidonPause() {

//TODOAuto-generated method stub

super.onPause();

if(getService) {

lms.removeUpdates(this);//離開頁面時停止更新

}

}

}

转载http://style77125tech.pixnet.net/blog/post/4324148-%5Bandroid%5D-gps-%E8%B3%87%E8%A8%8A%E6%93%B7%E5%8F%96

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值