-
return false;
-
}
-
/**
-
* 检测是否在正确的移动
-
*
-
* @param distance
-
* @return
-
*/
-
private boolean checkProperMove(float distance){
-
if(distance <= 0.1 * discard){
-
return true;
-
}else{
-
return false;
-
}
-
}
-
/**
-
* 检测获取的数据是否是正常的
-
*
-
* @param location
-
* @return
-
*/
-
private boolean checkProperLocation(BDLocation location){
-
if (location != null && location.getLatitude() != 0 && location.getLongitude() != 0){
-
return true;
-
}
-
return false;
-
}
-
@Override
-
public String call() throws Exception {
-
synchronized (lock) {
-
if (!checkProperLocation(location)){
-
LogUtil.info(LocationService.class, “location data is null”);
-
discard++;
-
return null;
-
}
-
if (MyApplication.orderDealInfoId != -1) {
-
DistanceInfo mDistanceInfo = mDistanceInfoDao.getById(MyApplication.orderDealInfoId); //根据MainActivity中赋值的全局id查询数据库的值
-
if(mDistanceInfo != null) //不为空则说明车已经开始行使,并可以获得经纬度,计算移动距离
-
{
-
LogUtil.info(LocationService.class, “行驶中…”);
-
GpsLocation tempGpsLocation = BDLocation2GpsUtil.convertWithBaiduAPI(location); //位置转换
-
if (tempGpsLocation != null) {
-
currentGpsLocation = tempGpsLocation;
-
}else{
-
discard ++;
-
}
-
//日志
-
String logMsg = “(plat:—>” + prevGpsLocation.lat + " plgt:—>" + prevGpsLocation.lng +“)\n” +
-
“(clat:—>” + currentGpsLocation.lat + " clgt:—>" + currentGpsLocation.lng + “)”;
-
LogUtil.info(LocationService.class, logMsg);
-
/** 计算距离 */
-
float distance = 0.0f;
-
DistanceComputeInterface distanceComputeInterface = DistanceComputeImpl.getInstance(); //计算距离类对象
-
distance = (float) distanceComputeInterface.getLongDistance(prevGpsLocation.lat,prevGpsLocation.lng,
-
currentGpsLocation.lat,currentGpsLocation.lng); //移动距离计算
-
if (!noMove(distance)) { //是否在移动
-
if (checkProperMove(distance)) { //合理的移动
-
float drivedDistance = mDistanceInfo.getDistance();
-
mDistanceInfo.setDistance(distance + drivedDistance); //拿到数据库原始距离值, 加上当前值
-
mDistanceInfo.setLongitude(currentGpsLocation.lng); //经度
-
mDistanceInfo.setLatitude(currentGpsLocation.lat); //纬度
-
//日志记录
-
FileUtils.saveToSDCard(FILE_NAME,“移动距离—>:”+distance+drivedDistance+“\n”+“数据库中保存的距离”+mDistanceInfo.getDistance());
-
mDistanceInfoDao.updateDistance(mDistanceInfo);
-
discard = 1;
-
}
-
}
-
prevGpsLocation = currentGpsLocation;
-
}
-
}
-
return null;
-
}
-
}
-
}
-
/**
-
* 定位SDK监听函数
-
*/
-
public class MyLocationListenner implements BDLocationListener {
-
@Override
-
public void onReceiveLocation(BDLocation location) {
-
executor.submit(new Task(location));
-
LogUtil.info(LocationService.class, “经度:”+location.getLongitude());
android中使用百度定位sdk实时的计算移动距离,安卓面试宝典
最新推荐文章于 2024-04-08 18:54:02 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)