android 获取经纬度

来源:互联网 发布:侠客风云传 战斗数据 编辑:程序博客网 时间:2024/05/18 03:31

主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> 
还有导入相应的库文件

0 0
原创粉丝点击