轉載自 http://www.open-open.com/lib/view/open1346982366162.html
使用Android自帶的LocationManager和Location獲取位置的時候,經常會有獲取的location爲null的情況,並且操作起來也不是很方便,在這個Demo裏我使用了百度地圖API中的定位SDK,可以一次性獲取當前位置經緯度以及詳細地址信息,還可以獲取周邊POI信息,同時可以設定位置通知點,當到達某一位置時,發出通知信息等方式來告知用戶。jar包下載以及官方文檔請參照:百度定位SDK,前提是需要註冊百度開發者賬號。
下面來看看定位的基本原理,目前,定位SDK可以通過GPS、基站、Wifi信號進行定位。基本定位流程如下圖所示,當應用程序向定位SDK發起定位請求時,定位SDK會根據當前的GPS、基站、Wifi信息生成相對應的定位依據。然後定位SDK會根據定位依據來進行定位。如果需要,定位SDK會向定位服務器發送網絡請求。定位服務器會根據請求的定位依據推算出對應的座標位置,然後根據用戶的定製信息,生成定位結果返回給定位SDK。
到官方下載jar文件後添加到工程,工程目錄截圖如下:
注意要把locSDK_2.4.jar添加到當天工程,右鍵jar文件-Build path-Add to。。。
上代碼
佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Start"/>
<TextView
android:id="@+id/tv_loc_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ericssonlabs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<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.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".LocationDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:permission="android.permission.BAIDU_LOCATION_SERVICE"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.4" />
</intent-filter>
</service>
</application>
</manifest>
實現代碼:
public class LocationDemoActivity extends Activity {
private TextView locationInfoTextView = null;
private Button startButton = null;
private LocationClient locationClient = null;
private static final int UPDATE_TIME = 5000;
private static int LOCATION_COUTNS = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info);
startButton = (Button) this.findViewById(R.id.btn_start);
locationClient = new LocationClient(this);
//設置定位條件
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); //是否打開GPS
option.setCoorType("bd09ll"); //設置返回值的座標類型。
option.setPriority(LocationClientOption.NetWorkFirst); //設置定位優先級
option.setProdName("LocationDemo"); //設置產品線名稱。強烈建議您使用自定義的產品線名稱,方便我們以後爲您提供更高效準確的定位服務。
option.setScanSpan(UPDATE_TIME); //設置定時定位的時間間隔。單位毫秒
locationClient.setLocOption(option);
//註冊位置監聽器
locationClient.registerLocationListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Time : ");
sb.append(location.getTime());
sb.append("\nError code : ");
sb.append(location.getLocType());
sb.append("\nLatitude : ");
sb.append(location.getLatitude());
sb.append("\nLontitude : ");
sb.append(location.getLongitude());
sb.append("\nRadius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nSpeed : ");
sb.append(location.getSpeed());
sb.append("\nSatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\nAddress : ");
sb.append(location.getAddrStr());
}
LOCATION_COUTNS ++;
sb.append("\n檢查位置更新次數:");
sb.append(String.valueOf(LOCATION_COUTNS));
locationInfoTextView.setText(sb.toString());
}
@Override
public void onReceivePoi(BDLocation location) {
}
});
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (locationClient == null) {
return;
}
if (locationClient.isStarted()) {
startButton.setText("Start");
locationClient.stop();
}else {
startButton.setText("Stop");
locationClient.start();
/*
*當所設的整數值大於等於1000(ms)時,定位SDK內部使用定時定位模式。
*調用requestLocation( )後,每隔設定的時間,定位SDK就會進行一次定位。
*如果定位SDK根據定位依據發現位置沒有發生變化,就不會發起網絡請求,
*返回上一次定位的結果;如果發現位置改變,就進行網絡請求進行定位,得到新的定位結果。
*定時定位時,調用一次requestLocation,會定時監聽到定位結果。
*/
locationClient.requestLocation();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationClient != null && locationClient.isStarted()) {
locationClient.stop();
locationClient = null;
}
}
}
來看看最後實現效果,點擊Start後進入位置監聽狀態,根據設置的監聽時間間隔進行定位,如果位置有變化則進行位置更新,同時顯示了檢測位置更新的次數,如果開啓了GPS,則獲取到衛星後,進行GPS定位:
設置位置提醒的功能我這裏就沒實現了,感興趣的可以參考開發指南