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> 

还有导入相应的库文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值