Android GPS服務標準版
GPS
定位系統由三部分組成,即由GPS
衛星組成的空間部分,若干地面組成的控制部分和普通使用者手中的接收機這三部分。對於手機使用者來說,手機就是GPS
定位系統的接收機,也就是說GPS
定位需要手機的硬體支援GPS
功能。
許可權
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
一、LocationManager
位置管理類,其主要功能包括:
// 根據LocationProvider獲取最近一次已知的Location Location getLastKnownLocation(String provider); //獲取所有的LocationProvider列表 List<String> getAllProviders(); //根據指定條件返回最優的LocationProvider物件 String getBestProvider(Criteria criteria,boolean enabledOnly); //根據名稱來獲取LocationProvider LocationProvider getProvider(String name): //根據指定條件獲取滿足該條件的全部LocationProvider的名稱 List<String>getProviders(Criteria criteria,boolean enabledOnly): // 獲取所有可用的LocationProvider List<String> getProviders(boolean enabledOnly): //判斷指定名稱的LocationProvider是否可用 boolean isProviderEnabled(provider): // 獲取GPS狀態 GpsStatus getGpsStatus(GpsStatus status); //新增一個監聽GPS狀態的監聽器 boolean addGpsStatusListener(GpsStatus.Listener listener); //刪除Gps狀態監聽器 void removeGpsStatusListener(GpsStatus.Listener listener): //通過指定的LoactionProvider週期性地獲取定位資訊,並通過intent啟動相應的元件 void requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent): // 通過指定的LocationProvider週期性地獲取定位資訊,並觸發listener所對應的觸發器 void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener): //新增一個臨近警告 void addProximityAlert(double latitude,doublelongitude,float radius, long expiration, PendingIntent intent); // 刪除一個臨近警告 void removeProximityAlert(PendingIntent intent):
二、LocationProvider
位置提供類,其主要功能包括:
//返回該LocationProvider的精度 int getAccuracy(); // 返回該LocationProvider的名稱 String getName(); //獲取該LocationProvider的電源需求 int getPowerRequirement(); //獲取該LocationProvider是收費的還是免費的 boolean hasMonetaryCost(); //判斷該LocationProvider是否滿足Criteria條件 boolean meetsCriteria(Criteria criteria) ; // 判斷該LocationProvider是否需要訪問網路基站 boolean requiresCell(); //判斷該LocationProvider是否需要網路資料 boolean requiresNetwork(); //判斷該LocationProvider是否需要訪問基於衛星的定位系統 boolean requiresSatellite(); //判斷該LocationProvider是否支援高度資訊 boolean supportsAltitude(); //判斷該LocationProvider是否支援方向資訊 boolean supportsBearing(); // 判斷該LocationProvider是否支援速度資訊 boolean supportsSpeed();
三、Location
代表位置的抽象類,主要方法包括:
//獲取定位資訊的精度 float getAccuracy(); //獲取定位資訊的高度 double getAltitude(); //獲取定位資訊的方向 float getBearing(); //獲取定位資訊的緯度 getLatitude(); // 獲取定位資訊的經度 getLongitude(); // 獲取提供該定位資訊的LocationProvider getProvider(); //獲取定位資訊的速度 getSpeed(); //判斷該定位資訊是否有精度資訊 hasAccuracy(); // 判斷該定位資訊是否有高度資訊 hasAltitude(); //判斷該定位資訊是否有方向資訊 hasBearing(); //判斷該定位資訊是否有速度資訊 hasSpeed();
以上三個類是Android GPS
的三個核心API
,用它們來獲取GPS
定位資訊的步驟為:
-
獲取系統的
LocationManager
物件。 -
使用
LocationManager
,通過指定LocationProvider
來獲取定位資訊,定位資訊由Location
物件來表示。 -
從
Location中
獲取定位資訊。
四、Criteria
除了以上三個核心類之外,Criteria
這個類也很重要。
LocationManager
的getBestProvider(Criteria criteria,boolean enabledOnly)
用來得到符合指定條件的LocationProvider
。Criteria
代表了一個過濾條件,提供如下常用方法來設定:
//設定對LocationProvider的精度要求 setAccuracy(int accuracy); //設定要求LocationProvider能提供高度資訊 setAltitudeRequired(boolean altitudeRequired); //設定要求LocationProvider能提供方向資訊 setBearingRequired(boolean bearingRequired); //設定要求LocationProvider是否免費 setCostAllowed(booleancostAllowed); //設定要求LocationProvider的耗電量 setPowerRequirement(int level); //設定要求LocationProvider能提供速度資訊 setSpeedRequired(boolean speedRequired);
五、基本使用
/** * 開始定位 */ public void startLocation(Context mContext, LocationCallback listener) { locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String best = locationManager.getBestProvider(criteria, true); if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling //ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding //public void onRequestPermissionsResult(int requestCode, String[] permissions, //int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } this.listener = listener; locationManager.requestLocationUpdates(best, intervalTime, 0, MyLocationListener); } /** * 位置監聽 */ private LocationListener MyLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (listener != null && location != null) { listener.onLocation(location.getLatitude() + "", location.getLongitude() + ""); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } };