package com.jqyd.android.module.lbs;
import java.text.ParseException;
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;
import com.jqyd.android.module.lbs.Interface.ILocationListener;
import com.jqyd.android.module.lbs.bean.LocationInfo;
import com.jqyd.android.module.lbs.util.CheckState_interface;
import com.jqyd.android.module.lbs.util.JqydDateUtil;
import com.jqyd.android.module.lbs.util.Optsharepre_interface;
import com.jqyd.android.module.lbs.util.WriteFile;
class Baidu_location {
public LocationClient mLocationClient = null;
private StringBuffer sb;
private LocationInfo bean = new LocationInfo();;
private ILocationListener lintener;
private LocationUtils util;
private CheckState_interface check;
private Optsharepre_interface share;
private WriteFile writeFile=null;
public StringBuffer getSb() {
return sb;
}
public MyLocationListenner myListener = new MyLocationListenner();
public Baidu_location(Context context, ILocationListener lintener) {
this.lintener = lintener;
check = new CheckState_interface(context);
share = new Optsharepre_interface(context);
writeFile=new WriteFile("LocationServiceLog"
+ JqydDateUtil.getDateDayOne(new Date()));
bean.setGpsStatus(check.checkGpState());
share.editPres("gpsStatus", bean.getGpsStatus()+"");
bean.setWlanStatus(check.checkWiFi());
share.editPres("wlanStatus", bean.getWlanStatus()+"");
bean.setMobileNetworkStatus(check.checkNet());
share.editPres("mobileNetworkStatus", bean.getMobileNetworkStatus()+"");
mLocationClient = new LocationClient(context.getApplicationContext());
mLocationClient.registerLocationListener(myListener);
setLocationOption();
startLoction();
Log.d("BDL", "BaiDuLocation 构造函数");
util = new LocationUtils(context);
}
// 设置相关参数
private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(false);// 关闭gps
option.disableCache(true);// 禁用缓存
option.setIsNeedAddress(true);
option.setCoorType("gcj02");// 返回国测局经纬度坐标系
mLocationClient.setLocOption(option);
}
// 开始定位
public void startLoction() {
writeFile.writeToFile("开始百度定位");
if (mLocationClient.isStarted() == false) {
mLocationClient.start();
}
mLocationClient.requestLocation();
}
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
writeFile.writeToFile("接收百度请求……"+location);
System.out.println("接收百度请求……" + location);
if (location == null) {
return;
}
try {
bean.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(location.getTime()).getTime());
share.editPres("time", bean.getTime()+"");
} catch (ParseException e) {
e.printStackTrace();
}
bean.setErrorcode(location.getLocType());
share.editPres("errorcode", bean.getErrorcode()+"");
bean.setLat(location.getLatitude());
share.editPres("lat", bean.getLat()+"");
bean.setLon(location.getLongitude());
share.editPres("lon", bean.getLon()+"");
bean.setRadius(location.getRadius());
share.editPres("radius", bean.getRadius()+"");
bean.setProvince(location.getProvince());
share.editPres("province", bean.getProvince());
bean.setCountry(location.getDistrict());
share.editPres("country", bean.getCountry());
bean.setAltitude(location.getAltitude());
share.editPres("altitude", bean.getAltitude()+"");
if (location.getCity() != null
&& location.getCity().contains("省直辖县级行政单位")) {
bean.setCity("省辖县");
share.editPres("city", bean.getCity());
} else {
bean.setCity(location.getCity());
share.editPres("city", bean.getCity());
}
if (location.getAddrStr() != null
&& location.getAddrStr().contains("省直辖县级行政单位")) {
bean.setContent(location.getAddrStr().replace("省直辖县级行政单位", "省辖县"));
share.editPres("content", bean.getContent());
} else {
bean.setContent(location.getAddrStr());
share.editPres("content", bean.getContent());
}
if (location.getLocType() == BDLocation.TypeGpsLocation) {
bean.setPosour(1);
share.editPres("dwModule", bean.getPosour()+"");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
bean.setPosour(3);
share.editPres("dwModule", bean.getPosour()+"");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
} else {
bean.setPosour(0);
share.editPres("dwModule", bean.getPosour()+"");
}
writeFile.writeToFile("百度搜索位置结束……"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println("百度搜索位置结束……"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
if (util != null) {
util.getBaiduLocationData(lintener, bean, Baidu_location.this);
}
}
@Override
public void onReceivePoi(BDLocation arg0) {
}
}
/**
* 销毁监听器
*/
public void stopListener() {
writeFile.writeToFile("销毁监听器");
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.stop();
mLocationClient = null;
}
}
}