android 獲取經緯度

主Application


package com.example.basic;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
/**
 * 主Application
 */
public class LocationApplication extends Application {
public LocationClient mLocationClient;
public GeofenceClient mGeofenceClient;
public MyLocationListener mMyLocationListener;
public TextView mLocationResult, logMsg;
public TextView trigger, exit;
public Vibrator mVibrator;
@Override
public void onCreate() {
super.onCreate();
mLocationClient = new LocationClient(this.getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
mGeofenceClient = new GeofenceClient(getApplicationContext());


mVibrator = (Vibrator) getApplicationContext().getSystemService(
Context.VIBRATOR_SERVICE);
}


/**
* 實現實位回調監聽
*/
public class MyLocationListener implements BDLocationListener {


@Override
public void onReceiveLocation(BDLocation location) {
// Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\naddr : ");
sb.append(location.getAddrStr());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
logMsg(sb.toString());
Log.i("address", sb.toString());


// 保存經緯度
SharedPreferences preferences = getSharedPreferences("testsss",
Context.MODE_PRIVATE);
Editor editor = preferences.edit();


editor.putString("latitude", Double
.toString(location.getLatitude()).toString());
editor.putString("longitude",
Double.toString(location.getLongitude()).toString());
editor.commit();


}


}


/**
* 顯示請求字符串

* @param str
*/
public void logMsg(String str) {
try {
if (mLocationResult != null)
mLocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 高精度地理圍欄回調

* @author jpren

*/


}



在ancivity中進行調用:



 public void getLocation() {


LocationClient mLocationClient;
mLocationClient = ((LocationApplication) getApplication()).mLocationClient;
LocationMode tempMode = LocationMode.Hight_Accuracy;
String tempcoor = "gcj02";


LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);// 設置定位模式
option.setCoorType(tempcoor);// 返回的定位結果是百度經緯度,默認值gcj02
int span = 1000;
option.setScanSpan(span);// 設置發起定位請求的間隔時間爲5000ms
mLocationClient.setLocOption(option);
mLocationClient.start();
}




備註:經過實驗是可以得的出來的,但是有時候會出現

4.9e-324這樣子的。
這個說明程序沒問題,只是百度定位失敗的原因,只要換個容易定位的地方就ok了,4.9e-324是百度默認精讀,就是定位的時候,他返回的BDLocation裏字段latitude默認值。

記得在mianfest中配置這個
 <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.1" />  
            </intent-filter>  
        </service> 

還有導入相應的庫文件

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章