package com.jqmobile.core.android.lbs.base;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
public class Baidu_location {
public LocationClient mLocationClient = null;
private StringBuffer sb;
public StringBuffer getSb() {
return sb;
}
public MyLocationListenner myListener = new MyLocationListenner();
public Baidu_location(Context context) {
mLocationClient = new LocationClient(context.getApplicationContext());
mLocationClient.registerLocationListener( myListener );
setLocationOption();
Log.d("BDL", "BaiDuLocation 构造函数");
}
//设置相关参数
private void setLocationOption(){
LocationClientOption option = new LocationClientOption();
option.setOpenGps(false);//关闭gps
option.disableCache(true);//禁用缓存
option.setCoorType("gcj02");//返回国测局经纬度坐标系
mLocationClient.setLocOption(option);
}
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
System.out.println("接收百度请求……"+location);
if (location == null){
return ;
}
sb=new StringBuffer(256);
sb.append("time ;");
sb.append(location.getTime());
sb.append(";error code ;");
sb.append(location.getLocType());
sb.append(";latitude ;");
sb.append(location.getLatitude());
sb.append(";lontitude ;");
sb.append(location.getLongitude());
sb.append(";radius ;");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append(";speed ;");
sb.append(location.getSpeed());
sb.append(";satellite ;");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append(";addr ;");
sb.append(location.getAddrStr());
}
System.out.println("百度获得位置信息打印----------------------------------------"+sb.toString());
System.out.println("百度搜索位置结束……"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
/**
* 销毁监听器
*/
public void stopListener(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
}
}