基站定位是根據通訊網絡基站信息進行定位的方法,此定位方法需要有較豐富的基站地理信息數據支持。高德地圖目前的基站數據庫支持 GSM 網絡和CDMA網絡。
定位機制
Android 平台主要使用三個 Class 和一個 interface 訪問定位功能:
- LocationManager 是定位的核心接口,應用程序首先調用 getSystemService,得到此類的實例,然后通過此類向系統申請定位支持。
- 定位方法有多種,每一種都封裝為一個LocationProvider 的子類,如 GPS 定位,Cell 定位,IP 定位等等。應用程序可以設定一個標准,讓系統幫自己選擇一個合適的LocationProvider,此標准的定義由類 Criteria 封裝。
- 接口LocationListener 定義了回調方法,由應用程序實現。當回調條件觸發時(比如,當前位置發生了改變),系統會調用此接口中的對應方法。
此三個類加一個接口為 Android 定位的基石,至於接口 GpsStatus.NmeaListener,GpsStatus.Listener,以及類 GpsStatus,GpsSatellite 僅與 GPS 定位相關。
高德地圖Android API移動定位直接使用了類Criteria,以及接口LocationListener,使用兩個Proxy:LocationManagerProxy 和 LocationProviderProxy分別重載了LocationManager 及 LocationProvider。這兩個Proxy包含的方法及常量定義,與對應的標准類基本一致(LocationManager 的構造方法除外)。應用程序通過使用API,可以無縫的在基站定位,WiFi 定位及GPS定位間切換,定位過程如下圖所示:
注意使用Cell定位的應用,需要添加權限 READ_PHONE_STATE 和 ACCESS_COARSE_LOCATION。
移動定位開發步驟
移動定位API在高德地圖Android API 中主要使用LocationManagerProxy 類、 Criteria 類和一個LocationListener 接口來實現定位功能。下面介紹移動定位的具體使用方法:
第一步,導入庫文件
右擊工程名選擇Build Path>Configure Build Path…>libraries,單擊對話框右側的“Add Externel JARs…”,選定MapApi.jar。在Android V2.2 或更早版本中,還需要在Order and Export 中選中對應的MapApi.jar,確定后返回,這樣用戶就可以在自己的程序中使用高德地圖Android API 了。
第二步,設置使用權限
必須在manifest.xml 中設置網絡定位服務的使用權限
| <!-- 定位相關權限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> |
第三步,Import 定位相關類
| import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import com.amap.mapapi.location.LocationManagerProxy; |
第四步,定位類的實現
聲明一個LocationManagerProxy對象locationManager ,在onCreate函數中初始化此LocationManagerProxy 對象locationManager。
| private LocationManagerProxy locationManager = null; //聲明LocationManagerProxy … locationManager = LocationManagerProxy.getInstance(this); //初始化LocationManagerProxy |
初始化一個Criteria 對象,設置相關參數,此對象自動匹配最適合的定位類型。調用enableMyLocation函數啟動定位模塊,disableMyLocation 函數關閉定位模塊。
| //根據Criteria 對象獲取提供位置信息的provider,並啟動定位模塊 public boolean enableMyLocation() {
boolean result = true; Criteria cri = new Criteria(); cri.setAccuracy(Criteria.ACCURACY_COARSE); cri.setAltitudeRequired(false); cri.setBearingRequired(false); cri.setCostAllowed(false); String bestProvider = locationManager.getBestProvider(cri, true); locationManager.requestLocationUpdates(bestProvider, 2000, 10, this); return result; } // 關閉定位模塊 public void disableMyLocation() { locationManager.removeUpdates(this);
locationManager =null; }
|
實現LocationListener 接口,用來監聽定位信息,獲取定位的經緯度坐標,在TextView 中顯示。
|
private TextView myLocation; //顯示經緯度字符串的文本視圖
Handler handler = new Handler() //接收消息 { public void handleMessage(Message msg) { myLocation.setText((String)msg.obj); } };
public class LocationManager_Demo extends Activity implements LocationListener {
//LocationListener 的監聽接口 @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub if (location != null) { Double geoLat = location.getLatitude(); Double geoLng = location.getLongitude(); String str =("定位成功:(" + geoLng + "," + geoLat + ")"); Message msg = new Message(); msg.obj=str; if(handler!=null) handler.sendMessage(msg);
} } }
|
具體示例代碼,請參照網站的Android相關下載示例代碼的location定位。
