/***************************************************

*TODO: description .

* @author: gao_chun

* @version: 1.0.0

*@remark: 转载请注明出处

**************************************************/


前段时间因为项目需求,通过百度定位adk写了一个实时更新距离的程序(类似大家坐的士时,车上的里程表),遇到很多技术点,总结了一下发表出来和大家相互学习。直接要求定位具体的位置应该是不难的,只需要引入百度定位adk,并配置相关参数就可以完成,显示百度地图也类似,但是如果需要不断的实时显示移动距离,GPS定位从一个点,到第二个点,从第二个点,到第三个点,从第三个点......,移动距离是多少呢?不得不说,要实现这种需求的确存在一定的难度。


目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示。
大家都知道定位有三种方式:GPS 、Wifi 、 基站 .
误差方面的话,使用GPS误差在10左右,Wifi则在20 - 300左右 ,而使用基站则误差在100 - 300左右的样子,因为在室内GPS是定位不到的,必须在室外,
而我们项目的需求正好需要使用GPS定位,所以我们这里设置GPS优先。车,不可能在室内跑吧。



使用技术点:
1.百度定位sdk
2.sqlite数据库(用于保存经纬度和实时更新的距离)
3.通过经纬度计算距离的算法方式
4.TimerTask 、Handler



大概思路:
1)创建项目,上传应用到百度定位sdk获得应用对应key,并配置定位服务成功。
2)将配置的定位代码块放入service中,使程序在后台不断更新经纬度
3)为应用创建数据库和相应的数据表,编写 增删改查 业务逻辑方法
4)编写界面,通过点击按钮控制是否开始计算距离,并引用数据库,初始化表数据,实时刷新界面
5)在service的定位代码块中计算距离,并将距离和经纬度实时的保存在数据库(注:只要经纬度发生改变,计算出来的距离就要进行保存)

6)界面的刷新显示

文章后附源码下载链接

以下是MainActivity中的代码,通过注释可以理解思路流程.

[java] view plain copy print ?
  1. packageapp.ui.activity;
  2. importjava.util.Timer;
  3. importjava.util.TimerTask;
  4. importandroid.content.Intent;
  5. importandroid.os.Bundle;
  6. importandroid.os.Handler;
  7. importandroid.os.Message;
  8. importandroid.view.View;
  9. importandroid.view.WindowManager;
  10. importandroid.widget.Button;
  11. importandroid.widget.TextView;
  12. importandroid.widget.Toast;
  13. importapp.db.DistanceInfoDao;
  14. importapp.model.DistanceInfo;
  15. importapp.service.LocationService;
  16. importapp.ui.ConfirmDialog;
  17. importapp.ui.MyApplication;
  18. importapp.ui.R;
  19. importapp.utils.ConstantValues;
  20. importapp.utils.LogUtil;
  21. importapp.utils.Utils;
  22. publicclassMainActivityextendsActivity{
  23. privateTextViewmTvDistance;//控件
  24. privateButtonmButton;
  25. privateTextViewmLng_lat;
  26. privatebooleanisStart=true;//是否开始计算移动距离
  27. privateDistanceInfoDaomDistanceInfoDao;//数据库
  28. privatevolatilebooleanisRefreshUI=true;//是否暂停刷新UI的标识
  29. privatestaticfinalintREFRESH_TIME=5000;//5秒刷新一次
  30. privateHandlerrefreshHandler=newHandler(){//刷新界面的Handler
  31. publicvoidhandleMessage(Messagemsg){
  32. switch(msg.what){
  33. caseConstantValues.REFRESH_UI:
  34. if(isRefreshUI){
  35. LogUtil.info(DistanceComputeActivity.class,"refreshui");
  36. DistanceInfomDistanceInfo=mDistanceInfoDao.getById(MyApplication.orderDealInfoId);
  37. LogUtil.info(DistanceComputeActivity.class,"界面刷新--->"+mDistanceInfo);
  38. if(mDistanceInfo!=null){
  39. mTvDistance.setText(String.valueOf(Utils.getValueWith2Suffix(mDistanceInfo.getDistance())));
  40. mLng_lat.setText("经:"+mDistanceInfo.getLongitude()+"纬:"+mDistanceInfo.getLatitude());
  41. mTvDistance.invalidate();
  42. mLng_lat.invalidate();
  43. }
  44. }
  45. break;
  46. }
  47. super.handleMessage(msg);
  48. }
  49. };
  50. //定时器,每5秒刷新一次UI
  51. privateTimerrefreshTimer=newTimer(true);
  52. privateTimerTaskrefreshTask=newTimerTask(){
  53. @Override
  54. publicvoidrun(){
  55. if(isRefreshUI){
  56. Messagemsg=refreshHandler.obtainMessage();
  57. msg.what=ConstantValues.REFRESH_UI;
  58. refreshHandler.sendMessage(msg);
  59. }
  60. }
  61. };
  62. @Override
  63. protectedvoidonCreate(BundlesavedInstanceState){
  64. super.onCreate(savedInstanceState);
  65. getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
  66. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//保持屏幕常亮
  67. setContentView(R.layout.activity_expensecompute);
  68. startService(newIntent(this,LocationService.class));//启动定位服务
  69. Toast.makeText(this,"已启动定位服务...",1).show();
  70. init();//初始化相应控件
  71. }
  72. privatevoidinit(){
  73. mTvDistance=(TextView)findViewById(R.id.tv_drive_distance);
  74. mDistanceInfoDao=newDistanceInfoDao(this);
  75. refreshTimer.schedule(refreshTask,0,REFRESH_TIME);
  76. mButton=(Button)findViewById(R.id.btn_start_drive);
  77. mLng_lat=(TextView)findViewById(R.id.longitude_Latitude);
  78. }
  79. @Override
  80. publicvoidonClick(Viewv){
  81. super.onClick(v);
  82. switch(v.getId()){
  83. caseR.id.btn_start_drive://计算距离按钮
  84. if(isStart)
  85. {
  86. mButton.setBackgroundResource(R.drawable.btn_selected);
  87. mButton.setText("结束计算");
  88. isStart=false;
  89. DistanceInfomDistanceInfo=newDistanceInfo();
  90. mDistanceInfo.setDistance(0f);//距离初始值
  91. mDistanceInfo.setLongitude(MyApplication.lng);//经度初始值
  92. mDistanceInfo.setLatitude(MyApplication.lat);//纬度初始值
  93. intid=mDistanceInfoDao.insertAndGet(mDistanceInfo);//将值插入数据库,并获得数据库中最大的id
  94. if(id!=-1){
  95. MyApplication.orderDealInfoId=id;//将id赋值到程序全局变量中(注:该id来决定是否计算移动距离)
  96. Toast.makeText(this,"已开始计算...",0).show();
  97. }else{
  98. Toast.makeText(this,"idis-1,无法执行距离计算代码块",0).show();
  99. }
  100. }else{
  101. //自定义提示框
  102. ConfirmDialogdialog=newConfirmDialog(this,R.style.dialogNoFrame){
  103. @Override
  104. publicvoidsetDialogContent(TextViewcontent){
  105. content.setVisibility(View.GONE);
  106. }
  107. @Override
  108. publicvoidsetDialogTitle(TextViewtitle){
  109. title.setText("确认结束计算距离?");
  110. }
  111. @Override
  112. publicvoidstartMission(){
  113. mButton.setBackgroundResource(R.drawable.btn_noselect);
  114. mButton.setText("开始计算");
  115. isStart=true;
  116. isRefreshUI=false;//停止界面刷新
  117. if(refreshTimer!=null){
  118. refreshTimer.cancel();
  119. refreshTimer=null;
  120. }
  121. mDistanceInfoDao.delete(MyApplication.orderDealInfoId);//删除id对应记录
  122. MyApplication.orderDealInfoId=-1;//停止定位计算
  123. Toast.makeText(DistanceComputeActivity.this,"已停止计算...",0).show();
  124. }
  125. };
  126. dialog.show();
  127. }
  128. break;
  129. }
  130. }
  131. }

以下是LocationService中的代码,即配置的百度定位sdk代码块,放在继承了service的类中 LocationService.java (方便程序在后台实时更新经纬度)

[java] view plain copy print ?
  1. packageapp.service;
  2. importjava.util.concurrent.Callable;
  3. importjava.util.concurrent.ExecutorService;
  4. importjava.util.concurrent.Executors;
  5. importandroid.app.Service;
  6. importandroid.content.Intent;
  7. importandroid.os.IBinder;
  8. importapp.db.DistanceInfoDao;
  9. importapp.model.GpsLocation;
  10. importapp.model.DistanceInfo;
  11. importapp.ui.MyApplication;
  12. importapp.utils.BDLocation2GpsUtil;
  13. importapp.utils.FileUtils;
  14. importapp.utils.LogUtil;
  15. importcom.baidu.location.BDLocation;
  16. importcom.baidu.location.BDLocationListener;
  17. importcom.baidu.location.LocationClient;
  18. importcom.baidu.location.LocationClientOption;
  19. importcom.computedistance.DistanceComputeInterface;
  20. importcom.computedistance.impl.DistanceComputeImpl;
  21. publicclassLocationServiceextendsService{
  22. publicstaticfinalStringFILE_NAME="log.txt";//日志
  23. LocationClientmLocClient;
  24. privateObjectlock=newObject();
  25. privatevolatileGpsLocationprevGpsLocation=newGpsLocation();//定位数据
  26. privatevolatileGpsLocationcurrentGpsLocation=newGpsLocation();
  27. privateMyLocationListennermyListener=newMyLocationListenner();
  28. privatevolatileintdiscard=1;//Volatile修饰的成员变量在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。
  29. privateDistanceInfoDaomDistanceInfoDao;
  30. privateExecutorServiceexecutor=Executors.newSingleThreadExecutor();
  31. @Override
  32. publicIBinderonBind(Intentintent){
  33. returnnull;
  34. }
  35. @Override
  36. publicvoidonCreate(){
  37. super.onCreate();
  38. mDistanceInfoDao=newDistanceInfoDao(this);//初始化数据库
  39. //LogUtil.info(LocationService.class,"Threadid----------->:"+Thread.currentThread().getId());
  40. mLocClient=newLocationClient(this);
  41. mLocClient.registerLocationListener(myListener);
  42. //定位参数设置
  43. LocationClientOptionoption=newLocationClientOption();
  44. option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
  45. option.setAddrType("all");//返回的定位结果包含地址信息
  46. option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
  47. option.disableCache(true);//禁止启用缓存定位
  48. option.setProdName("app.ui.activity");
  49. option.setOpenGps(true);
  50. option.setPriority(LocationClientOption.GpsFirst);//设置GPS优先
  51. mLocClient.setLocOption(option);
  52. mLocClient.start();
  53. mLocClient.requestLocation();
  54. }
  55. @Override
  56. @Deprecated
  57. publicvoidonStart(Intentintent,intstartId){
  58. super.onStart(intent,startId);
  59. }
  60. @Override
  61. publicvoidonDestroy(){
  62. super.onDestroy();
  63. if(null!=mLocClient){
  64. mLocClient.stop();
  65. }
  66. startService(newIntent(this,LocationService.class));
  67. }
  68. privateclassTaskimplementsCallable<String>{
  69. privateBDLocationlocation;
  70. publicTask(BDLocationlocation){
  71. this.location=location;
  72. }
  73. /**
  74. *检测是否在原地不动
  75. *
  76. *@paramdistance
  77. *@return
  78. */
  79. privatebooleannoMove(floatdistance){
  80. if(distance<0.01){
  81. returntrue;
  82. }
  83. returnfalse;
  84. }
  85. /**
  86. *检测是否在正确的移动
  87. *
  88. *@paramdistance
  89. *@return
  90. */
  91. privatebooleancheckProperMove(floatdistance){
  92. if(distance<=0.1*discard){
  93. returntrue;
  94. }else{
  95. returnfalse;
  96. }
  97. }
  98. /**
  99. *检测获取的数据是否是正常的
  100. *
  101. *@paramlocation
  102. *@return
  103. */
  104. privatebooleancheckProperLocation(BDLocationlocation){
  105. if(location!=null&&location.getLatitude()!=0&&location.getLongitude()!=0){
  106. returntrue;
  107. }
  108. returnfalse;
  109. }
  110. @Override
  111. publicStringcall()throwsException{
  112. synchronized(lock){
  113. if(!checkProperLocation(location)){
  114. LogUtil.info(LocationService.class,"locationdataisnull");
  115. discard++;
  116. returnnull;
  117. }
  118. if(MyApplication.orderDealInfoId!=-1){
  119. DistanceInfomDistanceInfo=mDistanceInfoDao.getById(MyApplication.orderDealInfoId);//根据MainActivity中赋值的全局id查询数据库的值
  120. if(mDistanceInfo!=null)//不为空则说明车已经开始行使,并可以获得经纬度,计算移动距离
  121. {
  122. LogUtil.info(LocationService.class,"行驶中......");
  123. GpsLocationtempGpsLocation=BDLocation2GpsUtil.convertWithBaiduAPI(location);//位置转换
  124. if(tempGpsLocation!=null){
  125. currentGpsLocation=tempGpsLocation;
  126. }else{
  127. discard++;
  128. }
  129. //日志
  130. StringlogMsg="(plat:--->"+prevGpsLocation.lat+"plgt:--->"+prevGpsLocation.lng+")\n"+
  131. "(clat:--->"+currentGpsLocation.lat+"clgt:--->"+currentGpsLocation.lng+")";
  132. LogUtil.info(LocationService.class,logMsg);
  133. /**计算距离*/
  134. floatdistance=0.0f;
  135. DistanceComputeInterfacedistanceComputeInterface=DistanceComputeImpl.getInstance();//计算距离类对象
  136. distance=(float)distanceComputeInterface.getLongDistance(prevGpsLocation.lat,prevGpsLocation.lng,
  137. currentGpsLocation.lat,currentGpsLocation.lng);//移动距离计算
  138. if(!noMove(distance)){//是否在移动
  139. if(checkProperMove(distance)){//合理的移动
  140. floatdrivedDistance=mDistanceInfo.getDistance();
  141. mDistanceInfo.setDistance(distance+drivedDistance);//拿到数据库原始距离值,加上当前值
  142. mDistanceInfo.setLongitude(currentGpsLocation.lng);//经度
  143. mDistanceInfo.setLatitude(currentGpsLocation.lat);//纬度
  144. //日志记录
  145. FileUtils.saveToSDCard(FILE_NAME,"移动距离--->:"+distance+drivedDistance+"\n"+"数据库中保存的距离"+mDistanceInfo.getDistance());
  146. mDistanceInfoDao.updateDistance(mDistanceInfo);
  147. discard=1;
  148. }
  149. }
  150. prevGpsLocation=currentGpsLocation;
  151. }
  152. }
  153. returnnull;
  154. }
  155. }
  156. }
  157. /**
  158. *定位SDK监听函数
  159. */
  160. publicclassMyLocationListennerimplementsBDLocationListener{
  161. @Override
  162. publicvoidonReceiveLocation(BDLocationlocation){
  163. executor.submit(newTask(location));
  164. LogUtil.info(LocationService.class,"经度:"+location.getLongitude());
  165. LogUtil.info(LocationService.class,"纬度:"+location.getLatitude());
  166. //将经纬度保存于全局变量,在MainActivity中点击按钮时初始化数据库字段
  167. if(MyApplication.lng<=0&&MyApplication.lat<=0)
  168. {
  169. MyApplication.lng=location.getLongitude();
  170. MyApplication.lat=location.getLatitude();
  171. }
  172. }
  173. publicvoidonReceivePoi(BDLocationpoiLocation){
  174. if(poiLocation==null){
  175. return;
  176. }
  177. }
  178. }
  179. }
以下是应用中需要使用的DBOpenHelper数据库类 DBOpenHelper.java

[java] view plain copy print ?
  1. packageapp.db;
  2. importandroid.content.Context;
  3. importandroid.database.sqlite.SQLiteDatabase;
  4. importandroid.database.sqlite.SQLiteOpenHelper;
  5. publicclassDBOpenHelperextendsSQLiteOpenHelper{
  6. privatestaticfinalintVERSION=1;//数据库版本号
  7. privatestaticfinalStringDB_NAME="distance.db";//数据库名
  8. publicDBOpenHelper(Contextcontext){//创建数据库
  9. super(context,DB_NAME,null,VERSION);
  10. }
  11. @Override
  12. publicvoidonCreate(SQLiteDatabasedb){//创建数据表
  13. db.execSQL("CREATETABLEIFNOTEXISTSmilestone(idINTEGERPRIMARYKEYAUTOINCREMENT,distanceINTEGER,longitudeDOUBLE,latitudeDOUBLE)");
  14. }
  15. @Override
  16. publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){//版本号发生改变的时
  17. db.execSQL("droptablemilestone");
  18. db.execSQL("CREATETABLEIFNOTEXISTSmilestone(idINTEGERPRIMARYKEYAUTOINCREMENT,distanceINTEGER,longitudeFLOAT,latitudeFLOAT)");
  19. }
  20. }

以下是应用中需要使用的数据库业务逻辑封装类 DistanceInfoDao.java

[java] view plain copy print ?
  1. packageapp.db;
  2. importandroid.content.Context;
  3. importandroid.database.Cursor;
  4. importandroid.database.sqlite.SQLiteDatabase;
  5. importapp.model.DistanceInfo;
  6. importapp.utils.LogUtil;
  7. publicclassDistanceInfoDao{
  8. privateDBOpenHelperhelper;
  9. privateSQLiteDatabasedb;
  10. publicDistanceInfoDao(Contextcontext){
  11. helper=newDBOpenHelper(context);
  12. }
  13. publicvoidinsert(DistanceInfomDistanceInfo){
  14. if(mDistanceInfo==null){
  15. return;
  16. }
  17. db=helper.getWritableDatabase();
  18. Stringsql="INSERTINTOmilestone(distance,longitude,latitude)VALUES('"+mDistanceInfo.getDistance()+"','"+mDistanceInfo.getLongitude()+"','"+mDistanceInfo.getLatitude()+"')";
  19. LogUtil.info(DistanceInfoDao.class,sql);
  20. db.execSQL(sql);
  21. db.close();
  22. }
  23. publicintgetMaxId(){
  24. db=helper.getReadableDatabase();
  25. Cursorcursor=db.rawQuery("SELECTMAX(id)asidfrommilestone",null);
  26. if(cursor.moveToFirst()){
  27. returncursor.getInt(cursor.getColumnIndex("id"));
  28. }
  29. return-1;
  30. }
  31. /**
  32. *添加数据
  33. *@paramorderDealInfo
  34. *@return
  35. */
  36. publicsynchronizedintinsertAndGet(DistanceInfomDistanceInfo){
  37. intresult=-1;
  38. insert(mDistanceInfo);
  39. result=getMaxId();
  40. returnresult;
  41. }
  42. /**
  43. *根据id获取
  44. *@paramid
  45. *@return
  46. */
  47. publicDistanceInfogetById(intid){
  48. db=helper.getReadableDatabase();
  49. Cursorcursor=db.rawQuery("SELECT*frommilestoneWHEREid=?",newString[]{String.valueOf(id)});
  50. DistanceInfomDistanceInfo=null;
  51. if(cursor.moveToFirst()){
  52. mDistanceInfo=newDistanceInfo();
  53. mDistanceInfo.setId(cursor.getInt(cursor.getColumnIndex("id")));
  54. mDistanceInfo.setDistance(cursor.getFloat(cursor.getColumnIndex("distance")));
  55. mDistanceInfo.setLongitude(cursor.getFloat(cursor.getColumnIndex("longitude")));
  56. mDistanceInfo.setLatitude(cursor.getFloat(cursor.getColumnIndex("latitude")));
  57. }
  58. cursor.close();
  59. db.close();
  60. returnmDistanceInfo;
  61. }
  62. /**
  63. *更新距离
  64. *@paramorderDealInfo
  65. */
  66. publicvoidupdateDistance(DistanceInfomDistanceInfo){
  67. if(mDistanceInfo==null){
  68. return;
  69. }
  70. db=helper.getWritableDatabase();
  71. Stringsql="updatemilestonesetdistance="+mDistanceInfo.getDistance()+",longitude="+mDistanceInfo.getLongitude()+",latitude="+mDistanceInfo.getLatitude()+"whereid="+mDistanceInfo.getId();
  72. LogUtil.info(DistanceInfoDao.class,sql);
  73. db.execSQL(sql);
  74. db.close();
  75. }
  76. }

以下是需要使用到的实体类 DistanceInfo.java (set数据到对应变量,以实体类作为参数更新数据库)

[java] view plain copy print ?
  1. packageapp.model;
  2. publicclassDistanceInfo{
  3. privateintid;
  4. privatefloatdistance;
  5. privatedoublelongitude;
  6. privatedoublelatitude;
  7. publicintgetId(){
  8. returnid;
  9. }
  10. publicvoidsetId(intid){
  11. this.id=id;
  12. }
  13. publicfloatgetDistance(){
  14. returndistance;
  15. }
  16. publicvoidsetDistance(floatdistance){
  17. this.distance=distance;
  18. }
  19. publicdoublegetLongitude(){
  20. returnlongitude;
  21. }
  22. publicvoidsetLongitude(doublelongitude){
  23. this.longitude=longitude;
  24. }
  25. publicdoublegetLatitude(){
  26. returnlatitude;
  27. }
  28. publicvoidsetLatitude(doublelatitude){
  29. this.latitude=latitude;
  30. }
  31. @Override
  32. publicStringtoString(){
  33. return"DistanceInfo[id="+id+",distance="+distance
  34. +",longitude="+longitude+",latitude="+latitude+"]";
  35. }
  36. }

保存经纬度信息的类 GpsLocation

[java] view plain copy print ?
  1. packageapp.model;
  2. publicclassGpsLocation{
  3. publicdoublelat;//纬度
  4. publicdoublelng;//经度
  5. }
将从百度定位中获得的经纬度转换为精准的GPS数据 BDLocation2GpsUtil.java

[java] view plain copy print ?
  1. packageapp.utils;
  2. importit.sauronsoftware.base64.Base64;
  3. importjava.io.BufferedReader;
  4. importjava.io.IOException;
  5. importjava.io.InputStreamReader;
  6. importjava.net.HttpURLConnection;
  7. importjava.net.URL;
  8. importorg.json.JSONObject;
  9. importapp.model.GpsLocation;
  10. importcom.baidu.location.BDLocation;
  11. publicclassBDLocation2GpsUtil{
  12. staticBDLocationtempBDLocation=newBDLocation();//临时变量,百度位置
  13. staticGpsLocationtempGPSLocation=newGpsLocation();//临时变量,gps位置
  14. publicstaticenumMethod{
  15. origin,correct;
  16. }
  17. privatestaticfinalMethodmethod=Method.correct;
  18. /**
  19. *位置转换
  20. *
  21. *@paramlBdLocation百度位置
  22. *@returnGPS位置
  23. */
  24. publicstaticGpsLocationconvertWithBaiduAPI(BDLocationlBdLocation){
  25. switch(method){
  26. caseorigin://原点
  27. GpsLocationlocation=newGpsLocation();
  28. location.lat=lBdLocation.getLatitude();
  29. location.lng=lBdLocation.getLongitude();
  30. returnlocation;
  31. casecorrect://纠偏
  32. //同一个地址不多次转换
  33. if(tempBDLocation.getLatitude()==lBdLocation.getLatitude()&&tempBDLocation.getLongitude()==lBdLocation.getLongitude()){
  34. returntempGPSLocation;
  35. }
  36. Stringurl="http://api.map.baidu.com/ag/coord/convert?from=0&to=4&"
  37. +"x="+lBdLocation.getLongitude()+"&y="
  38. +lBdLocation.getLatitude();
  39. Stringresult=executeHttpGet(url);
  40. LogUtil.info(BDLocation2GpsUtil.class,"result:"+result);
  41. if(result!=null){
  42. GpsLocationgpsLocation=newGpsLocation();
  43. try{
  44. JSONObjectjsonObj=newJSONObject(result);
  45. StringlngString=jsonObj.getString("x");
  46. StringlatString=jsonObj.getString("y");
  47. //解码
  48. doublelng=Double.parseDouble(newString(Base64.decode(lngString)));
  49. doublelat=Double.parseDouble(newString(Base64.decode(latString)));
  50. //换算
  51. gpsLocation.lng=2*lBdLocation.getLongitude()-lng;
  52. gpsLocation.lat=2*lBdLocation.getLatitude()-lat;
  53. tempGPSLocation=gpsLocation;
  54. LogUtil.info(BDLocation2GpsUtil.class,"result:"+gpsLocation.lat+"||"+gpsLocation.lng);
  55. }catch(Exceptione){
  56. e.printStackTrace();
  57. returnnull;
  58. }
  59. tempBDLocation=lBdLocation;
  60. returngpsLocation;
  61. }else{
  62. LogUtil.info(BDLocation2GpsUtil.class,"百度API执行出错,urlis:"+url);
  63. returnnull;
  64. }
  65. }
  66. }
  67. }

需要声明相关权限,且项目中所用到的jar有:
android-support-v4.jar
commons-codec.jar
commons-lang3-3.0-beta.jar
javabase64-1.3.1.jar
locSDK_3.1.jar


Android中计算地图上两点距离的算法


项目中目前尚有部分不健全的地方,如:
1.在行驶等待时间较长后,使用TimerTask 、Handler刷新界面是偶尔会出现卡住的现象,车仍在行驶,
但是数据不动了,通过改善目前测试近7次未出现此问题。

2.较快的消耗电量

源码下载地址


【转载注明gao_chun的Blog:http://blog.csdn.net/gao_chun/article/details/38229339】


更多相关文章

  1. android中如何获取经纬度?
  2. Android下载文件时对MediaScanner的调用
  3. Android(安卓)SQLite数据库判断某张表是否存在的语句
  4. Android中使用SQLiteDatabase对数据库进行操作
  5. 经纬度转度分秒 Java/Android
  6. Android数据库编程:SqLiteOpenHelper的使用
  7. Android/J2SE计算两个位置坐标之间的距离
  8. Android(安卓)获取经纬度同时获取当前具体城市信息

随机推荐

  1. Android(安卓)如何将定制的Launcher成为
  2. Android(安卓)adb shell 命令
  3. Android(安卓)UI之RelativeLayout(相对布
  4. Android:网页设计界面
  5. Android应用开发入门五问
  6. Android(安卓)M 6.0,关于ActivityThread
  7. Android(安卓)高级面试
  8. android 学习笔记一
  9. android中能不能new Activity()对象引发
  10. Android开发便签7:如何让通讯录匹配N位号