2016-03-24 11:54:41 1536瀏覽
在目前隨著移動互聯(lián)網(wǎng)科技飛速的發(fā)展,如今Android用戶在中國更是達(dá)到80%以上的市場占有率,火爆的市場帶來了Android開發(fā)人員旺盛的需求,下面我們來介紹一個關(guān)于Android開發(fā)實(shí)戰(zhàn)之使用GPS定位技術(shù)獲取當(dāng)前位置的信息的案例及代碼吧。
1、在AndroidManifest.xml中添加ACCESS_FINE_LOCATION權(quán)限,具體代碼如下所示。
2、編寫主文件main.xml,用于創(chuàng)建用戶界面,具體代碼如下所示。
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
3、在onCreate(Bundle savedInstanceState)中獲取當(dāng)前位置信息,具體代碼如下所示。
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
StringserviceName=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(serviceName);
//Stringprovider=LocationManager.GPS_PROVIDER;
Criteria criteria=newCriteria;
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
Stringprovider=locationManager.getBestProvider(criteria,true);
Location location=locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
/*每隔1000ms更新一次,并且不考慮位置的變化。*/
locationManager.requestLocationUpdates(provider,2000,10,
locationListener);
}
在上述代碼中,LocationManager用于周期獲得當(dāng)前設(shè)備的一個類。要獲取LocationManager實(shí)例,需要調(diào)用Context.getSystemService方法并傳入服務(wù)名LOCATION_SERVICE("location")。創(chuàng)建LocationManager實(shí)例后,就可以通過調(diào)用getLastKnownLocation方法將上一次LocationManager獲得的有效位置信息以Location對象的形式返回。getLastKnownLocation方法需要傳入一個字符串參數(shù)來確定使用定位服務(wù)類型,本實(shí)例傳入的是靜態(tài)常量LocationManager.GPS_PROVIDER,這表示使用GPS技術(shù)定位。最后還需要使用Location對象將位置信息以文本方式顯示到用戶界面。
4、定義方法updateWithNewLocation(Location location),用于更新顯示用戶界面。具體代碼如下所示。
void updateWithNewLocation(Location location) {
StringlatLongString;
TextView myLocationText;
myLocationText=(TextView)findViewById(R.id.myLocationText);
if(location !=null) {
doublelat=location.getLatitude;
doublelng=location.getLongitude;
latLongString="緯度:"+lat+"\n經(jīng)度:"+lng;
}else{
latLongString="無法獲取地理信息";
}
myLocationText.setText("您當(dāng)前的位置是:\n"+
latLongString);
}
}
5、定義LocationListener對象locationListener,當(dāng)坐標(biāo)改變時觸發(fā)此函數(shù)。如果Provider傳進(jìn)相同的坐標(biāo),它就不會被觸發(fā)。
final LocationListener locationListener=newLocationListener {
publicvoid onLocationChanged(Location location) {
updateWithNewLocation(location);
}
publicvoid onProviderDisabled(Stringprovider){
updateWithNewLocation(null);
}
publicvoid onProviderEnabled(Stringprovider){ }
publicvoid onStatusChanged(Stringprovider,intstatus,
Bundle extras){ }
};
至此整個實(shí)例介紹完畢。因?yàn)槟M器上沒有GPS設(shè)備,所以需要在eclipse的DDMS工具中提供模擬的GPS數(shù)據(jù)。即依次單擊“DDMS”︱“Emulator Control”,在彈出對話框中找到“Location Control”選項(xiàng),在此輸入坐標(biāo),完成后單擊“Send”按鈕。
因?yàn)橛玫搅薌oogle API,所以要在項(xiàng)目中引入Google API,郵件單擊項(xiàng)目選擇“Properties”,在彈出對話框中選擇Google API版本。
查看更多關(guān)于“Android培訓(xùn)資訊”的相關(guān)文章>>