在上文中,介紹瞭GPS概念及Android開發GPS應用涉及到的常用類和方法。在本文中,開發一個小應用,實時獲取定位信息,包括用戶所在的緯度、經度、高度、方向、移動速度等。代碼如下:
Activity:
[java]
package comhome.location;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class LocationTestActivity extends Activity {
// 定義LocationManager對象
private LocationManager locationManager;
private EditText show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (EditText) findViewById(R.id.main_et_show);
// 獲取系統LocationManager服務
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 從GPS獲取最近的定位信息
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 將location裡的位置信息顯示在EditText中
updateView(location);
// 設置每2秒獲取一次GPS的定位信息
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 8, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// 當GPS定位信息發生改變時,更新位置
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
updateView(null);
}
@Override
public void onProviderEnabled(String provider) {
// 當GPS LocationProvider可用時,更新位置
updateView(locationManager
.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
});
}
private void updateView(Location location) {
if (location != null) {
StringBuffer sb = new StringBuffer();
sb.append("實時的位置信息:\n經度:");
sb.append(location.getLongitude());
sb.append("\n緯度:");
sb.append(location.getLatitude());
sb.append("\n高度:");
sb.append(location.getAltitude());
sb.append("\n速度:");
sb.append(location.getSpeed());
sb.append("\n方向:");
sb.append(location.getBearing());
sb.append("\n精度:");
sb.append(location.getAccuracy());
show.setText(sb.toString());
} else {
// 如果傳入的Location對象為空則清空EditText
show.setText("");
}
}
}
package comhome.location;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class LocationTestActivity extends Activity {
// 定義LocationManager對象
private LocationManager locationManager;
private EditText show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (EditText) findViewById(R.id.main_et_show);
// 獲取系統LocationManager服務
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 從GPS獲取最近的定位信息
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 將location裡的位置信息顯示在EditText中
updateView(location);
// 設置每2秒獲取一次GPS的定位信息
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 8, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// 當GPS定位信息發生改變時,更新位置
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
updateView(null);
}
@Override
public void onProviderEnabled(String provider) {
// 當GPS LocationProvider可用時,更新位置
updateView(locationManager
.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
});
}
private void updateView(Location location) {
if (location != null) {
StringBuffer sb = new StringBuffer();
sb.append("實時的位置信息:\n經度:");
sb.append(location.getLongitude());
sb.append("\n緯度:");
sb.append(location.getLatitude());
sb.append("\n高度:");
sb.append(location.getAltitude());
sb.append("\n速度:");
sb.append(location.getSpeed());
sb.append("\n方向:");
sb.append(location.getBearing());
sb.append("\n精度:");
sb.append(location.getAccuracy());
show.setText(sb.toString());
} else {
// 如果傳入的Location對象為空則清空EditText
show.setText("");
}
}
}
佈局XML:
[html]
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/main_et_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:editable="false" />
</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/main_et_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:editable="false" />
</LinearLayout>權限:
[html]
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>附上圖片效果: