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

Android 定位技術解析:網路定位、基站定位與 GPS

編輯:關於 Android 程式設計

手機定位並不是單一技術,而是三種機制的組合與互補。理解各自的原理與極限,才能在實作時選對策略。

一、網路定位(IP 定位)

前提是裝置必須連上網路——Wi-Fi、3G、2G 皆可。取得 IP 位址後,比對後端的 IP 對地理位置資料庫,就能推出一個大致位置。早期的彩虹版 QQ、珊瑚蟲版 QQ 之所以能顯示對方 IP,靠的就是這個特性;警方早期的辦案手法也有類似思路。

局限:只適用於固定 IP。當電信網路或 Wi-Fi 採用動態配置 IP 時,推算出來的位置可能偏差數百公里,完全無法滿足需求。

二、基站定位

手機能通話,靠的是基地台;定位同樣可以借用基地台。當裝置能同時收到三個以上基地台的訊號,透過多邊定位演算法即可算出座標。

精度:基地台密集的市區相當準確,偏遠地區基地台稀疏時誤差就明顯放大,整體範圍從數十公尺到數公里不等。

三、GPS 與 A-GPS

A-GPS 結合衛星定位與網路輔助:裝置會優先請求網路,取得先前快取的衛星軌道資訊,藉此大幅縮短首次定位時間。

限制:需要搜尋衛星,頭頂必須開闊。雲層、大樓、樹蔭都會造成訊號衰減或遮蔽,室內幾乎無法使用。

四、程式實作

Android 以 LocationManager 搭配 Criteria 來挑選最佳定位提供者:

package com.andy.gpsdemo;

import com.example.gpsdemo.R;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

    // 位置服務
    private LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        /* 若想列出裝置支援的所有提供者,可這樣做:
        List<String> provider = locationManager.getAllProviders();
        for (String string : provider) {
            System.out.println(string);
        } */

        // 為位置提供者設定條件
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);      // 要求最高精度
        // criteria.setAltitudeRequired(false);            // 不要求海拔
        // criteria.setBearingRequired(false);             // 不要求方位
        // criteria.setCostAllowed(true);                  // 允許付費定位
        // criteria.setPowerRequirement(Criteria.POWER_LOW); // 優先省電

        String provider = locationManager.getBestProvider(criteria, true);

        locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                String longitude = "經度:" + location.getLongitude();
                String latitude  = "維度:" + location.getLatitude();
                String accuracy  = "精確度:" + location.getAccuracy();

                TextView textView = new TextView(MainActivity.this);
                textView.setText(longitude + "\n" + latitude + "\n" + accuracy);
                setContentView(textView);
            }

            @Override
            public void onProviderDisabled(String provider) {
                // 提供者被停用
            }

            @Override
            public void onProviderEnabled(String provider) {
                // 提供者恢復可用
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // 狀態切換時回呼:開啟→關閉、關閉→開啟
            }
        });
    }
}

五、版面配置

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.andy.gpsdemo.MainActivity" >

    <TextView
        android:textSize="22sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

六、必要權限

<!-- 模擬器測試用 -->
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<!-- 精確定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 粗略定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

實作建議

三種技術各有取捨:網路定位快但粗略、基站定位在都會區表現不差、GPS 最準卻耗電且受限於環境。實務上常見做法是透過 Criteria 把精度與電耗需求描述清楚,交給系統動態挑選,並在 onProviderDisabled 中做好降級處理。此外,Android 6.0 以後 these 定位權限屬於執行期危險權限,必須在執行階段動態向使用者申請,否則 getLastKnownLocation 與位置更新都會拿到空值。

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