android 百度轨迹记录,Android使用百度LBS SDK(四)记录和显示行走轨迹

记录轨迹思路用Service获取经纬度,onCreate中开始采集经纬度点,保存到ArrayList每隔5秒取样一次,若经纬度未发生变化,丢弃该次取样在onDestroy中,将ArrayLis

记录轨迹思路

显示轨迹思路

初步Demo效果图,,获取的经纬度有偏移,明天看看哪里的问题:

e750b03856a9b14d39680aa8c9811a17.png

先贴一个保存经纬度点的Service的核心代码:

{

private LocationClient mLocationClient;

private final String ROUTE_PATH = "/sdcard/Route/";

private String startTime = "";

private String stopTime = "";

private List list = new ArrayList();

private RouteAdapter adapter = new RouteAdapter();

defaultDelay = ERROR_CODE = 55.555;

private double routeLng;

private double routeLat;

isDebug = true;

DeviceLocType {

LATITUDE, LONGITUDE

}

@Override

public IBinder onBind(Intent intent) {

;

}

() {

super.onCreate();

InitLocation(LocationMode.Hight_Accuracy, "gcj02", 1000, false);

// 初始化路径

File filestoreMusic = new File(ROUTE_PATH);

if (!filestoreMusic.exists()) {

filestoreMusic.mkdir();

}

startTime = getTimeStr();

if (isDebug) {

Toast.makeText(getApplicationContext(), "Start Record Route",

Toast.LENGTH_SHORT).show();

}

// 开启轨迹记录线程

new Thread(new RouteRecordThread()).start();

}

{

() {

while (true) {

try {

Thread.sleep(defaultDelay);

Message message = new Message();

message.what = 1;

recordHandler.sendMessage(message);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

final Handler recordHandler = new Handler() {

(Message msg) {

switch (msg.what) {

case 1:

startRecordRoute();

}

super.handleMessage(msg);

}

};

() {

// 获取设备经纬度

if (!isEncrypt) {

routeLat = getDeviceLocation(DeviceLocType.LATITUDE);

routeLng = getDeviceLocation(DeviceLocType.LONGITUDE);

if (isDebug)

Toast.makeText(getApplicationContext(),

"Device Loc:" + routeLat + "," + routeLng,

Toast.LENGTH_SHORT).show();

}

RoutePoint routePoint = new RoutePoint();

if (routeLng != 5.55 && routeLat != 5.55) {

if (list.size() > 0

&& list.get(list.size() - 1).getLat() == routeLat

&& (list.get(list.size() - 1).getLng() == routeLng)) {

if (isDebug) {

}

} else {

routePoint.setId(startId++);

routePoint.setLng(routeLng);

routePoint.setLat(routeLat);

list.add(routePoint);

}

}

}

/**

* 获取设备提供的经纬度,Network或GPS

*

*@param type

*

请求经度还是纬度

*@return

*/

(DeviceLocType type) {

double deviceLat = ERROR_CODE;

double deviceLng = ERROR_CODE;

LocationManager locationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

Location location = locationManager

.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

deviceLat = location.getLatitude();

deviceLng = location.getLongitude();

} else {

locationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER, 1000, 0,

new deviceLocationListener());

Location location1 = locationManager

.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location1 != null) {

deviceLat = location1.getLatitude(); // 经度

deviceLng = location1.getLongitude(); // 纬度

}

}

} else {

locationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER, 1000, 0,

new deviceLocationListener());

Location location = locationManager

.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {

deviceLat = location.getLatitude(); // 经度

deviceLng = location.getLongitude(); // 纬度

}

}

if (type == DeviceLocType.LATITUDE)

return deviceLat;

else if (type == DeviceLocType.LONGITUDE)

return deviceLng;

else

return ERROR_CODE;

}

/**

* 设备位置监听器

*

*/

class deviceLocationListener implements LocationListener {

(String provider, int status, Bundle extras) {

}

(String provider) {

}

(String provider) {

}

(Location location) {

// routeLat = location.getLatitude(); // 经度

// routeLng = location.getLongitude(); // 纬度

}

};

private String getTimeStr() {

long nowTime = System.currentTimeMillis();

Date date = new Date(nowTime);

String strs = "" + ERROR_CODE;

try {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");

strs = sdf.format(date);

} catch (Exception e) {

e.printStackTrace();

}

return strs;

}

/**

* 初始化轨迹文件路径和名称

*

*@return String

*/

private String getFilePath() {

stopTime = getTimeStr();

String format = ".json";

if (isDebug)

format = ".txt";

return ROUTE_PATH + startTime + "-" + stopTime + format;

}

class MyLocationListener implements BDLocationListener {

(BDLocation location) {

// 读取百度加密经纬度

if (isEncrypt) {

routeLng = location.getLongitude();

routeLat = location.getLatitude();

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值