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

记录轨迹思路显示轨迹思路

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

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

{private LocationClient mLocationClient;private final String ROUTE_PATH = “/sdcard/Route/”;private String startTime = “”;private String stopTime = “”;private List<RoutePoint> list = new ArrayList<RoutePoint>();private RouteAdapter adapter = new RouteAdapter();defaultDelay = ERROR_CODE = 55.555;private double routeLng;private double routeLat;isDebug = true;DeviceLocType {LATITUDE, LONGITUDE}@Overridepublic 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;elsereturn 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();}}}} 只有经历过地狱般的折磨,才有征服天堂的力量,只有流过血的手指,才能弹出世间的绝唱。

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

相关文章:

你感兴趣的文章:

标签云: