前段时间因为项目需求,通过百度定位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中的代码,通过注释可以理解思路流程.
-
package app.ui.activity;
-
import java.util.Timer;
-
import java.util.TimerTask;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.view.View;
-
import android.view.WindowManager;
-
import android.widget.Button;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
import app.db.DistanceInfoDao;
-
import app.model.DistanceInfo;
-
import app.service.LocationService;
-
import app.ui.ConfirmDialog;
-
import app.ui.MyApplication;
-
import app.ui.R;
-
import app.utils.ConstantValues;
-
import app.utils.LogUtil;
-
import app.utils.Utils;
-
-
public class MainActivity extends Activity {
-
-
private TextView mTvDistance;
-
private Button mButton;
-
private TextView mLng_lat;
-
private boolean isStart = true;
-
-
private DistanceInfoDao mDistanceInfoDao;
-
private volatile boolean isRefreshUI = true;
-
private static final int REFRESH_TIME = 5000;
-
-
private Handler refreshHandler = new Handler(){
-
public void handleMessage(Message msg) {
-
switch (msg.what) {
-
case ConstantValues.REFRESH_UI:
-
if (isRefreshUI) {
-
LogUtil.info(DistanceComputeActivity.class, "refresh ui");
-
DistanceInfo mDistanceInfo = mDistanceInfoDao.getById(MyApplication.orderDealInfoId);
-
LogUtil.info(DistanceComputeActivity.class, "界面刷新---> "+mDistanceInfo);
-
if (mDistanceInfo != null) {
-
mTvDistance.setText(String.valueOf(Utils.getValueWith2Suffix(mDistanceInfo.getDistance())));
-
mLng_lat.setText("经:"+mDistanceInfo.getLongitude()+" 纬:"+mDistanceInfo.getLatitude());
-
mTvDistance.invalidate();
-
mLng_lat.invalidate();
-
}
-
}
-
break;
-
}
-
super.handleMessage(msg);
-
}
-
};
-
-
-
private Timer refreshTimer = new Timer(true);
-
private TimerTask refreshTask = new TimerTask() {
-
@Override
-
public void run() {
-
if (isRefreshUI) {
-
Message msg = refreshHandler.obtainMessage();
-
msg.what = ConstantValues.REFRESH_UI;
-
refreshHandler.sendMessage(msg);
-
}
-
}
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
-
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-
setContentView(R.layout.activity_expensecompute);
-
-
startService(new Intent(this,LocationService.class));
-
Toast.makeText(this,"已启动定位服务...", 1).show();
-
init();
-
}
-
-
private void init(){
-
mTvDistance = (TextView) findViewById(R.id.tv_drive_distance);
-
mDistanceInfoDao = new DistanceInfoDao(this);
-
refreshTimer.schedule(refreshTask, 0, REFRESH_TIME);
-
mButton = (Button)findViewById(R.id.btn_start_drive);
-
mLng_lat = (TextView)findViewById(R.id.longitude_Latitude);
-
}
-
-
-
@Override
-
public void onClick(View v) {
-
super.onClick(v);
-
switch (v.getId()) {
-
case R.id.btn_start_drive:
-
if(isStart)
-
{
-
mButton.setBackgroundResource(R.drawable.btn_selected);
-
mButton.setText("结束计算");
-
isStart = false;
-
DistanceInfo mDistanceInfo = new DistanceInfo();
-
mDistanceInfo.setDistance(0f);
-
mDistanceInfo.setLongitude(MyApplication.lng);
-
mDistanceInfo.setLatitude(MyApplication.lat);
-
int id = mDistanceInfoDao.insertAndGet(mDistanceInfo);
-
if (id != -1) {
-
MyApplication.orderDealInfoId = id;
-
Toast.makeText(this,"已开始计算...", 0).show();
-
}else{
-
Toast.makeText(this,"id is -1,无法执行距离计算代码块", 0).show();
-
}
-
}else{
-
-
ConfirmDialog dialog = new ConfirmDialog(this, R.style.dialogNoFrame){
-
@Override
-
public void setDialogContent(TextView content) {
-
content.setVisibility(View.GONE);
-
}
-
@Override
-
public void setDialogTitle(TextView title) {
-
title.setText("确认结束计算距离 ?");
-
}
-
@Override
-
public void startMission() {
-
mButton.setBackgroundResource(R.drawable.btn_noselect);
-
mButton.setText("开始计算");
-
isStart = true;
-
isRefreshUI = false;
-
if (refreshTimer != null) {
-
refreshTimer.cancel();
-
refreshTimer = null;
-
}
-
mDistanceInfoDao.delete(MyApplication.orderDealInfoId);
-
MyApplication.orderDealInfoId = -1;
-
Toast.makeText(DistanceComputeActivity.this,"已停止计算...", 0).show();
-
}
-
};
-
dialog.show();
-
}
-
break;
-
}
-
}
-
}
以下是LocationService中的代码,即配置的百度定位sdk代码块,放在继承了service的类中 LocationService.java (方便程序在后台实时更新经纬度)
-
package app.service;
-
import java.util.concurrent.Callable;
-
import java.util.concurrent.ExecutorService;
-
import java.util.concurrent.Executors;
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.IBinder;
-
import app.db.DistanceInfoDao;
-
import app.model.GpsLocation;
-
import app.model.DistanceInfo;
-
import app.ui.MyApplication;
-
import app.utils.BDLocation2GpsUtil;
-
import app.utils.FileUtils;
-
import app.utils.LogUtil;
-
import com.baidu.location.BDLocation;
-
import com.baidu.location.BDLocationListener;
-
import com.baidu.location.LocationClient;
-
import com.baidu.location.LocationClientOption;
-
import com.computedistance.DistanceComputeInterface;
-
import com.computedistance.impl.DistanceComputeImpl;
-
-
public class LocationService extends Service {
-
-
public static final String FILE_NAME = "log.txt";
-
LocationClient mLocClient;
-
private Object lock = new Object();
-
private volatile GpsLocation prevGpsLocation = new GpsLocation();
-
private volatile GpsLocation currentGpsLocation = new GpsLocation();
-
private MyLocationListenner myListener = new MyLocationListenner();
-
private volatile int discard = 1;
-
private DistanceInfoDao mDistanceInfoDao;
-
private ExecutorService executor = Executors.newSingleThreadExecutor();
-
-
@Override
-
public IBinder onBind(Intent intent) {
-
return null;
-
}
-
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
mDistanceInfoDao = new DistanceInfoDao(this);
-
-
mLocClient = new LocationClient(this);
-
mLocClient.registerLocationListener(myListener);
-
-
LocationClientOption option = new LocationClientOption();
-
option.setCoorType("bd09ll");
-
option.setAddrType("all");
-
option.setScanSpan(5000);
-
option.disableCache(true);
-
option.setProdName("app.ui.activity");
-
option.setOpenGps(true);
-
option.setPriority(LocationClientOption.GpsFirst);
-
mLocClient.setLocOption(option);
-
mLocClient.start();
-
mLocClient.requestLocation();
-
-
}
-
-
@Override
-
@Deprecated
-
public void onStart(Intent intent, int startId) {
-
super.onStart(intent, startId);
-
}
-
-
@Override
-
public void onDestroy() {
-
super.onDestroy();
-
if (null != mLocClient) {
-
mLocClient.stop();
-
}
-
startService(new Intent(this, LocationService.class));
-
}
-
-
private class Task implements Callable<String>{
-
private BDLocation location;
-
public Task(BDLocation location){
-
this.location = location;
-
}
-
-
-
-
-
-
-
-
private boolean noMove(float distance){
-
if (distance < 0.01) {
-
return true;
-
}
-
return false;
-
}
-
-
-
-
-
-
-
-
private boolean checkProperMove(float distance){
-
if(distance <= 0.1 * discard){
-
return true;
-
}else{
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
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);
-
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;
-
}
-
}
-
}
-
-
-
-
-
public class MyLocationListenner implements BDLocationListener {
-
@Override
-
public void onReceiveLocation(BDLocation location) {
-
executor.submit(new Task(location));
-
-
LogUtil.info(LocationService.class, "经度:"+location.getLongitude());
-
LogUtil.info(LocationService.class, "纬度:"+location.getLatitude());
-
-
if(MyApplication.lng <=0 && MyApplication.lat <= 0)
-
{
-
MyApplication.lng = location.getLongitude();
-
MyApplication.lat = location.getLatitude();
-
}
-
}
-
-
public void onReceivePoi(BDLocation poiLocation) {
-
if (poiLocation == null){
-
return ;
-
}
-
}
-
}
-
}
以下是应用中需要使用的DBOpenHelper数据库类 DBOpenHelper.java
-
package app.db;
-
import android.content.Context;
-
import android.database.sqlite.SQLiteDatabase;
-
import android.database.sqlite.SQLiteOpenHelper;
-
-
public class DBOpenHelper extends SQLiteOpenHelper{
-
private static final int VERSION = 1;
-
private static final String DB_NAME = "distance.db";
-
-
public DBOpenHelper(Context context){
-
super(context, DB_NAME, null, VERSION);
-
}
-
-
@Override
-
public void onCreate(SQLiteDatabase db) {
-
db.execSQL("CREATE TABLE IF NOT EXISTS milestone(id INTEGER PRIMARY KEY AUTOINCREMENT, distance INTEGER,longitude DOUBLE, latitude DOUBLE )");
-
}
-
-
@Override
-
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
db.execSQL("drop table milestone");
-
db.execSQL("CREATE TABLE IF NOT EXISTS milestone(id INTEGER PRIMARY KEY AUTOINCREMENT, distance INTEGER,longitude FLOAT, latitude FLOAT )");
-
}
-
-
}
以下是应用中需要使用的数据库业务逻辑封装类 DistanceInfoDao.java
-
package app.db;
-
import android.content.Context;
-
import android.database.Cursor;
-
import android.database.sqlite.SQLiteDatabase;
-
import app.model.DistanceInfo;
-
import app.utils.LogUtil;
-
-
public class DistanceInfoDao {
-
private DBOpenHelper helper;
-
private SQLiteDatabase db;
-
-
public DistanceInfoDao(Context context) {
-
helper = new DBOpenHelper(context);
-
}
-
-
public void insert(DistanceInfo mDistanceInfo) {
-
if (mDistanceInfo == null) {
-
return;
-
}
-
db = helper.getWritableDatabase();
-
String sql = "INSERT INTO milestone(distance,longitude,latitude) VALUES(‘"+ mDistanceInfo.getDistance() + "‘,‘"+ mDistanceInfo.getLongitude() + "‘,‘"+ mDistanceInfo.getLatitude() + "‘)";
-
LogUtil.info(DistanceInfoDao.class, sql);
-
db.execSQL(sql);
-
db.close();
-
}
-
-
public int getMaxId() {
-
db = helper.getReadableDatabase();
-
Cursor cursor = db.rawQuery("SELECT MAX(id) as id from milestone",null);
-
if (cursor.moveToFirst()) {
-
return cursor.getInt(cursor.getColumnIndex("id"));
-
}
-
return -1;
-
}
-
-
-
-
-
-
-
public synchronized int insertAndGet(DistanceInfo mDistanceInfo) {
-
int result = -1;
-
insert(mDistanceInfo);
-
result = getMaxId();
-
return result;
-
}
-
-
-
-
-
-
-
public DistanceInfo getById(int id) {
-
db = helper.getReadableDatabase();
-
Cursor cursor = db.rawQuery("SELECT * from milestone WHERE id = ?",new String[] { String.valueOf(id) });
-
DistanceInfo mDistanceInfo = null;
-
if (cursor.moveToFirst()) {
-
mDistanceInfo = new DistanceInfo();
-
mDistanceInfo.setId(cursor.getInt(cursor.getColumnIndex("id")));
-
mDistanceInfo.setDistance(cursor.getFloat(cursor.getColumnIndex("distance")));
-
mDistanceInfo.setLongitude(cursor.getFloat(cursor.getColumnIndex("longitude")));
-
mDistanceInfo.setLatitude(cursor.getFloat(cursor.getColumnIndex("latitude")));
-
}
-
cursor.close();
-
db.close();
-
return mDistanceInfo;
-
}
-
-
-
-
-
-
public void updateDistance(DistanceInfo mDistanceInfo) {
-
if (mDistanceInfo == null) {
-
return;
-
}
-
db = helper.getWritableDatabase();
-
String sql = "update milestone set distance="+ mDistanceInfo.getDistance() +",longitude="+mDistanceInfo.getLongitude()+",latitude="+mDistanceInfo.getLatitude()+" where id = "+ mDistanceInfo.getId();
-
LogUtil.info(DistanceInfoDao.class, sql);
-
db.execSQL(sql);
-
db.close();
-
}
-
}
以下是需要使用到的实体类 DistanceInfo.java (set数据到对应变量,以实体类作为参数更新数据库)
-
package app.model;
-
public class DistanceInfo {
-
private int id;
-
private float distance;
-
private double longitude;
-
private double latitude;
-
-
public int getId() {
-
return id;
-
}
-
public void setId(int id) {
-
this.id = id;
-
}
-
public float getDistance() {
-
return distance;
-
}
-
public void setDistance(float distance) {
-
this.distance = distance;
-
}
-
public double getLongitude() {
-
-
return longitude;
-
}
-
public void setLongitude(double longitude) {
-
-
this.longitude = longitude;
-
}
-
public double getLatitude() {
-
-
return latitude;
-
}
-
public void setLatitude(double latitude) {
-
-
this.latitude = latitude;
-
}
-
@Override
-
public String toString() {
-
-
return "DistanceInfo [id=" + id + ", distance=" + distance
-
+ ", longitude=" + longitude + ", latitude=" + latitude + "]";
-
}
-
}
保存经纬度信息的类 GpsLocation
-
package app.model;
-
public class GpsLocation {
-
public double lat;
-
public double lng;
-
}
将从百度定位中获得的经纬度转换为精准的GPS数据 BDLocation2GpsUtil.java
-
package app.utils;
-
import it.sauronsoftware.base64.Base64;
-
import java.io.BufferedReader;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
import org.json.JSONObject;
-
import app.model.GpsLocation;
-
import com.baidu.location.BDLocation;
-
public class BDLocation2GpsUtil {
-
static BDLocation tempBDLocation = new BDLocation();
-
static GpsLocation tempGPSLocation = new GpsLocation();
-
public static enum Method{
-
origin, correct;
-
}
-
private static final Method method = Method.correct;
-
-
-
-
-
-
-
public static GpsLocation convertWithBaiduAPI(BDLocation lBdLocation) {
-
switch (method) {
-
case origin:
-
GpsLocation location = new GpsLocation();
-
location.lat = lBdLocation.getLatitude();
-
location.lng = lBdLocation.getLongitude();
-
return location;
-
case correct:
-
-
if (tempBDLocation.getLatitude() == lBdLocation.getLatitude() && tempBDLocation.getLongitude() == lBdLocation.getLongitude()) {
-
return tempGPSLocation;
-
}
-
String url = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&"
-
+ "x=" + lBdLocation.getLongitude() + "&y="
-
+ lBdLocation.getLatitude();
-
String result = executeHttpGet(url);
-
LogUtil.info(BDLocation2GpsUtil.class, "result:" + result);
-
if (result != null) {
-
GpsLocation gpsLocation = new GpsLocation();
-
try {
-
JSONObject jsonObj = new JSONObject(result);
-
String lngString = jsonObj.getString("x");
-
String latString = jsonObj.getString("y");
-
-
double lng = Double.parseDouble(new String(Base64.decode(lngString)));
-
double lat = Double.parseDouble(new String(Base64.decode(latString)));
-
-
gpsLocation.lng = 2 * lBdLocation.getLongitude() - lng;
-
gpsLocation.lat = 2 * lBdLocation.getLatitude() - lat;
-
tempGPSLocation = gpsLocation;
-
LogUtil.info(BDLocation2GpsUtil.class, "result:" + gpsLocation.lat + "||" + gpsLocation.lng);
-
} catch (Exception e) {
-
e.printStackTrace();
-
return null;
-
}
-
tempBDLocation = lBdLocation;
-
return gpsLocation;
-
}else{
-
LogUtil.info(BDLocation2GpsUtil.class, "百度API执行出错,url is:" + url);
-
return null;
-
}
-
}
-
}
-
}
需要声明相关权限,且项目中所用到的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.较快的消耗电量
源码下载地址
android中使用百度定位sdk实时的计算移动距离,布布扣,bubuko.com
android中使用百度定位sdk实时的计算移动距离
原文:http://blog.csdn.net/gao_chun/article/details/38229339