編輯:關於 Android 程式設計
手機定位並不是單一技術,而是三種機制的組合與互補。理解各自的原理與極限,才能在實作時選對策略。
前提是裝置必須連上網路——Wi-Fi、3G、2G 皆可。取得 IP 位址後,比對後端的 IP 對地理位置資料庫,就能推出一個大致位置。早期的彩虹版 QQ、珊瑚蟲版 QQ 之所以能顯示對方 IP,靠的就是這個特性;警方早期的辦案手法也有類似思路。
局限:只適用於固定 IP。當電信網路或 Wi-Fi 採用動態配置 IP 時,推算出來的位置可能偏差數百公里,完全無法滿足需求。
手機能通話,靠的是基地台;定位同樣可以借用基地台。當裝置能同時收到三個以上基地台的訊號,透過多邊定位演算法即可算出座標。
精度:基地台密集的市區相當準確,偏遠地區基地台稀疏時誤差就明顯放大,整體範圍從數十公尺到數公里不等。
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 與位置更新都會拿到空值。
Android 屬性動畫完全指南:從 ValueAnimator 到 AnimatorSet
一、三種動畫,各自的位置 Android 提供了 View Animation、Drawable Animation 與 Property Animation 三套動畫體
智能廚房重構筆記(二):用 MVP 架構馴服失控的 Activity
上一篇介紹了專案的分包結構,這篇來談 MVP 架構在專案中的實際落地。 MVP 可以視為 MVC 的一種升級。在 MVP 普及之前大家都寫 MVC,但那套做法有個致命缺點
Android 視覺規範:間距與字型的單位換算(dip、sp 與 px)
一、dip 與 px Android 工程師撰寫頁面時,margin 值的單位是 dip,而視覺設計師通常以 px 控制 margin 值。在 WVGA(800x480)
Android NDK 開發環境建置流程
新版 NDK 已大幅簡化開發環境設定,不再一定需要手動安裝 Cygwin。過去使用 NDK 時,常需下載 NDK、Cygwin、CDT 與設定編譯器環境變數,但新版本已整