Record track Ideas
- Get latitude and longitude with service, start collecting latitude and longitude points in OnCreate, save to ArrayList
- Samples are sampled every 5 seconds, and if the latitude and longitude are not changed, the sampling is discarded
- In OnDestroy, ArrayList is converted to JSON format and stored in SDcard
Show track Ideas
- Read all the trace files in the directory and generate the ListView
- Transfer the file name through Intent.putextra to the activity that displays the track in Onitemclick
- Convert the JSON content to ArrayList according to the file name
- Then the above ArrayList point set line, and draw to the Baidu map
- Set start point marker,zoom level, center point as Start point
- Track points less than 2 can not draw the trajectory, give a hint
Preliminary demo, get the latitude and longitude has offset, tomorrow to see where the problem:
Paste the core code of the service that holds the latitude and longitude points first:
Public class routerecordservice extends Service { PrivateLocationclient mlocationclient;Private FinalString Route_path ="/sdcard/route/";PrivateString StartTime ="";PrivateString StopTime ="";Privatelist<routepoint> list =NewArraylist<routepoint> ();PrivateRouteadapter adapter =NewRouteadapter ();Private intStartid =1;//Track point initial ID Private intDefaultdelay = the;//Trajectory point sampling interval: Ms Private Final Static DoubleError_code =55.555;Private DoubleROUTELNG;Private DoubleRoutelat;Private BooleanIsencrypt =false;//true: Read Baidu encryption latitude and longitude false: Read device provides latitude and longitude Private BooleanIsdebug =true;//device positioning latitude and longitude Private enumDeviceloctype {LATITUDE, longitude}@Override PublicIBinderOnbind(Intent Intent) {//TODO auto-generated method stub return NULL; }@Override Public void onCreate() {Super. OnCreate (); Initlocation (Locationmode.hight_accuracy,"Gcj02", +,false);//Initialize pathFile Filestoremusic =NewFile (Route_path);if(!filestoremusic.exists ()) {Filestoremusic.mkdir (); } startTime = Gettimestr ();if(Isdebug) {Toast.maketext (Getapplicationcontext (),"Start Record Route", Toast.length_short). Show (); }//Open track record thread NewThread (NewRouterecordthread ()). Start (); } Public class routerecordthread implements Runnable { @Override Public void Run() { while(true) {Try{Thread.Sleep (Defaultdelay); Message message =NewMessage (); Message.what =1; Recordhandler.sendmessage (message); }Catch(Exception e) {E.printstacktrace (); } } } }FinalHandler Recordhandler =NewHandler () { Public void Handlemessage(Message msg) {Switch(msg.what) { Case 1: Startrecordroute (); }Super. Handlemessage (msg); } };Private void Startrecordroute() {//acquisition of device latitude and longitude 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 =NewRoutepoint ();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) {//Toast.maketext (Getapplicationcontext (), //"Route not change", //Toast.length_short). Show ();} }Else{Routepoint.setid (startid++); ROUTEPOINT.SETLNG (ROUTELNG); Routepoint.setlat (Routelat); List.add (Routepoint); } } }/** * Obtain the latitude and longitude provided by the device, network or GPS * * @param type * Request Longitude or Latitude * @return */ Private Double getdevicelocation(Deviceloctype type) {DoubleDevicelat = Error_code;DoubleDEVICELNG = 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, +,0,NewDevicelocationlistener ()); Location location1 = Locationmanager. Getlastknownlocation (Locationmanager.network_provider);if(Location1! =NULL) {Devicelat = Location1.getlatitude ();//LongitudeDEVICELNG = Location1.getlongitude ();//Latitude} } }Else{Locationmanager.requestlocationupdates (Locationmanager.network_provider, +,0,NewDevicelocationlistener ()); Location location = Locationmanager. Getlastknownlocation (Locationmanager.network_provider);if(Location! =NULL) {Devicelat = Location.getlatitude ();//LongitudeDEVICELNG = Location.getlongitude ();//Latitude} }if(type = = Deviceloctype.latitude)returnDevicelat;Else if(type = = Deviceloctype.longitude)returnDEVICELNG;Else returnError_code; }/** * Device Location Listener * */Class Devicelocationlistener implements Locationlistener {///The provider state triggers this function when it is switched directly between available, temporarily unavailable, and service-free states Public void onstatuschanged(String provider,intStatus, Bundle extras) {}///Provider triggers this function when enable, e.g. GPS is turned on @Override Public void onproviderenabled(String Provider) { }This function is triggered when the provider is disable, such as when the GPS is closed @Override Public void onproviderdisabled(String Provider) { }This function is triggered when coordinates change, and if provider is passed into the same coordinates, it will not be triggered @Override Public void onlocationchanged(Location location) {//Routelat = Location.getlatitude ();//Longitude //routelng = Location.getlongitude ();//Latitude} };PrivateStringGettimestr() {LongNowtime = System.currenttimemillis (); Date Date =NewDate (Nowtime); String STRs =""+ Error_code;Try{SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyymmdd_hhmmss"); STRs = Sdf.format (date); }Catch(Exception e) {E.printstacktrace (); }returnSTRs; }/** * Initialize trace file path and name * * @return String */ PrivateStringGetFilePath() {stoptime = Gettimestr (); String format =". JSON";if(isdebug) format =". txt";returnRoute_path + StartTime +"-"+ stoptime + format; } class Mylocationlistener implements Bdlocationlistener {@Override Public void onreceivelocation(bdlocation location) {//Read Baidu encryption latitude and longitude if(Isencrypt) {routelng = Location.getlongitude (); Routelat = Location.getlatitude (); } } }}
I'm in these places: CSDN GitHub Weibo
Android uses Baidu lbs SDK (iv) to record and display the walking track