记录轨迹思路用Service获取经纬度,onCreate中开始采集经纬度点,保存到ArrayList每隔5秒取样一次,若经纬度未发生变化,丢弃该次取样在onDestroy中,将ArrayLis
记录轨迹思路
显示轨迹思路
初步Demo效果图,,获取的经纬度有偏移,明天看看哪里的问题:
先贴一个保存经纬度点的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();
}
}
}
}