Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android GPS 應用程式設計完整指南

編輯:關於 Android 程式設計

GPS 簡介

GPS 是英文 Global Positioning System(全球定位系統)的簡稱,是 20 世紀 70 年代由美國陸海空三軍聯合研制的新一代空間衛星導航定位系統,主要為陸、海、空三大領域提供實時、全天候、全球性的導航服務,並用於情報收集、核爆監測和應急通訊等軍事目的。到 1994 年 3 月,全球覆蓋率高達 98% 的 24 顆 GPS 衛星星座已布設完成。

GPS 定位原理

GPS 定位的基本原理是根據高速運動的衛星瞬間位置作為已知的起算數據,採用空間距離後方交會的方法,確定待測點的位置。在待測點安置 GPS 接收機,測定 GPS 訊號到達接收機的時間,再加上接收到的衛星星歷等數據,即可建立方程式求解位置。

取得 LocationManager

// 獲取 LocationManager 對象
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 獲取所有的 LocationProvider 的名稱
List<String> providerNames = mLocationManager.getAllProviders();

透過名稱獲取指定 LocationProvider:

// 獲取基於 GPS 的 LocationProvider
LocationProvider locProvider = lm.getProvider(LocationManager.GPS_PROVIDER);

根據 Criteria 獲得 LocationProvider:

mProviders = (ListView) findViewById(R.id.providers);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria cri = new Criteria();
cri.setCostAllowed(false);       // 要求免費
cri.setAltitudeRequired(true);   // 要求提供高度
cri.setBearingRequired(true);    // 要求提供方向
List<String> providerNames = mLocationManager.getProviders(cri, true);

獲取定位數據

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 從 GPS 獲取最近的定位信息
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateView(location);
// 設置每 2 秒獲取一次 GPS 定位信息
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        updateView(location);
    }

    @Override
    public void onProviderDisabled(String provider) {
        updateView(null);
    }

    @Override
    public void onProviderEnabled(String provider) {
        updateView(mLocationManager.getLastKnownLocation(provider));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
});

該程式需要訪問 GPS 訊號的權限,因此需在 AndroidManifest.xml 中增加授權:

<!-- 授權獲取定位信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

趨近警告(Proximity Alert)

LocationManager locationManager;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
double longitude = 123.427109;
double latitude = 41.764998;
float radius = 2000; // 半徑 2000 米
Intent intent = new Intent(this, ProximityAlertReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
locationManager.addProximityAlert(latitude, longitude, radius, -1, pendingIntent);

對應的 BroadcastReceiver:

public class ProximityAlertReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isEnter = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
        if (isEnter) {
            Toast.makeText(context, "您已經接近萬裡學院4號樓", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "您已經離開萬裡學院4號樓", Toast.LENGTH_LONG).show();
        }
    }
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

計算兩點距離

final float[] results = new float[3];
Location.distanceBetween(20.123, 30.05644, 30.124, 40.543, results);
final float bearing = results[1];
mEditText.setText(String.valueOf(results[0]) + "米");

若將本程式與 Google Map 結合,讓程式根據 GPS 提供的資訊即時顯示使用者在地圖上的位置,即可進一步開發 GPS 導航系統。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved