main.xmljava
<?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:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> </LinearLayout>
LocationTest.javaandroid
package org.crazyit.gps; 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; /** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class LocationTest extends Activity { // 定義LocationManager對象 LocationManager locManager; // 定義程序界面中的EditText組件 EditText show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 獲取程序界面上的EditText組件 show = (EditText) findViewById(R.id.show); // 建立LocationManager對象 locManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); // 從GPS獲取最近的最近的定位信息 Location location = locManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); // 使用location根據EditText的顯示 updateView(location); // 設置每3秒獲取一次GPS的定位信息 locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 3000, 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(locManager .getLastKnownLocation(provider)); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }); } // 更新EditText中顯示的內容 public void updateView(Location newLocation) { if (newLocation != null) { StringBuilder sb = new StringBuilder(); sb.append("實時的位置信息:\n"); sb.append("經度:"); sb.append(newLocation.getLongitude()); sb.append("\n緯度:"); sb.append(newLocation.getLatitude()); sb.append("\n高度:"); sb.append(newLocation.getAltitude()); sb.append("\n速度:"); sb.append(newLocation.getSpeed()); sb.append("\n方向:"); sb.append(newLocation.getBearing()); show.setText(sb.toString()); } else { // 若是傳入的Location對象爲空則清空EditText show.setText(""); } } }
上面的程序中粗體字代碼用於從Location中獲取定位信息,包括用戶的經度、緯度、高度、方向和移動速度等信息。程序中①號粗體寧代碼經過LocationManager設置了一個監聽器,該}監聽器負責每隔3秒向LocationProvider請求一次定位信息,當LocationProvider可用時、不可用時或提供的定位信息發生改變時,系統會回調updateView(Location newLocation)來更新EditText中漫示的定位信息。
該程序須要有訪問GPS信號的權限,所以須要在AndroidManifest.xml文件中增長以下受權代碼片斷: git
<!-- 受權獲取定位信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />app
截圖:ide