編輯:關於Android編程
GPS簡介
GPS是英文Global Positioning System(全球定位系統)的簡稱。GPS是20世紀70年代由美國陸海空三軍聯合研制的
新一代空間衛星導航定位系統 。其主要目的是為陸、海、空三大領域提供實時、 全天候和全球性的導航服務,並用於
情報收集、核爆監測和應急通訊等一些軍事目的,經過20余年的研究實驗,耗資300億美元,到1994年3月,全球覆蓋
率高達98%的24顆GPS衛星星座己布設完成。
GPS定位原理
GPS定位的基本原理是根據高速運動的衛星瞬間位置作為已知的起算數據,采用空間距離後方交會的方法,確定待測
點的位置。如圖所示,假設t時刻在地面待測點上安置GPS接收機,可以測定GPS信號到達接收機的時間△t,再加上接
收機所接收到的衛星星歷等其它數據可以確定以下四個方程式)
vcyozOG5qcHL0ru49mFuZHJvaWQubG9jYXRpb261xLD8LNPQOLj2wOChozxzdHJvbmc+usvQxLXEM7j2wOA6PC9zdHJvbmc+PC9wPgo8cD5Mb2NhdGlvbk1hbmFnZXLA4KO61/fTw9PrV2lmaU1hbmFnZXKholRlbGVwaG9ueU1hbmFnZXK1yLf+zvHA4LXE1/fTw8/gJiMyMDI4NDujrMv509DT60dQU7aozrvP4LnYtcS3/s7xoaI8L3A+CjxwPrbUz/O2vNPJuMO21M/zsvrJ+qGjPC9wPgo8cD5Mb2NhdGlvblByb3ZpZGVywOCjuraozrvM4bmp1d+jrEdQU7aozrvX6bz+tcSz6c/zse3KvqOszai5/bjDttTP87/J0tS78cihuMO2qM671+m8/rXEz+C52NDFz6KhozwvcD4KPHA+TG9jYXRpb27A4KO60ru49rT6se3Ou9bD0MXPorXEs+nP88DgoaM8L3A+CjxwPjxicj4KPHN0cm9uZz7KudPDy/zDx8C0u/HIoUdQU7aozrvQxc+itcTNqNPDsr3W6M6qo7o8L3N0cm9uZz48L3A+CjxwPrvxyKHPtc2zTG9jYXRpb25NYW5hZ2VyttTP8zwvcD4KPHA+yrnTw0xvY2F0aW9uTWFuYWdlcqOszai5/da4tqi1xLaozrvJ6LG4TG9jYXRpb25Qcm92aWR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7unoaM8L3A+CjxwPjxicj4KPC9wPgo8cD7A/dfTtPrC6zo8L3A+CjxwPrvxyKFMb2NhdGlvblByb3ZpZGVyOjwvcD4KPHA+u/HIocv509C1xExvY2F0aW9uUHJvdmlkZXK1xMP7s8Y8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;"> // 獲取LocationManager對象
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 獲取所有的LocationProvider的名稱
List 獲取定位數據: 個監聽器,該監聽器負責每隔3秒向LocationProvider請求一次定位信息,當LocationProvider可用時,不可用時或提供的定 位信息發生改變時,系統會回調對應的方法更新界面. 如果該程序與Google Map結合,讓該程序根據GPS提供的信息實時地顯示用戶在地圖上的位置,即可開發GPS導航系統 計算兩點距離:
//獲取基於GPS的LocationProvider
LocationProvider locProvider = lm.getProvider(LocatinManager.GPS_PROVIDER)
根據Criteria獲得LocationProvider
mProviders = (ListView) findViewById(R.id.providers);
// 獲取系統的LocationManager對象
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 創建一個LocationProvider的過濾條件
Criteria cri = new Criteria();
// 設置要求LocationProvider必須是免費的。
cri.setCostAllowed(false);
// 設置要求LocationProvider能提供高度信息
cri.setAltitudeRequired(true);
// 設置要求LocationProvider能提供方向信息
cri.setBearingRequired(true);
// 獲取系統所有符合條件的LocationProvider的名稱
List
// 創建LocationManager對象
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 從GPS獲取最近的定位信息
Location location = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 使用location根據EditText的顯示
updateView(location);
// 設置每2秒獲取一次GPS的定位信息
mLocationManager.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(mLocationManager
.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());
mEditText.setText(sb.toString());
} else {
// 如果傳入的Location對象為空則清空EditText
mEditText.setText("");
}
該程序需要有訪問GPS信號的權限,因此需要在AndroidManifest.xml文件中增加授權信息.
用於從Location中獲取定位信息,包括用戶的經度,緯度,高度,方向,和移動速度等信息,並通過LocationManager設置了
趨近警告:// 定位服務管理器對象
LocationManager locationManager;
// 通過getSystemService方法獲得LocationManager對象
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 定義萬裡學院4號樓的大致經度、緯度
double longitude = 123.427109;
double latitude = 41.764998;
// 定義半徑(2000米)
float radius = 2000;
// 定義Intent
Intent intent = new Intent(this, ProximityAlertReciever.class);
// 將Intent包裝成PendingIntent
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();
}
}
}
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]) + "米");
由於android系統中應用程序之間不能共享內存。因此,在不同應用程序之間交互數據(跨進程通訊)就稍微麻煩一些。在android SDK中提供了4種用於跨進程通訊的方式。
自定義View一直是橫在Android開發者面前的一道坎。一、View和ViewGroup的關系從View和ViewGroup的關系來看,ViewGroup繼承View。
初始化對象private Paint mPaint;//畫筆 private int count;//點擊次數 private Rect rect;//矩形 public
1、概述Drawable在我們平時的開發中,基本都會用到,而且給大家非常的有用。那麼什麼是Drawable呢?能夠在canvas上繪制的一個玩意,而且相比於View,並不