preface
In the use of maps, especially in navigation scenes, GPS track recording is very necessary and useful. This paper will share the track recording part under Android system.
System architecture
For a GPS record system (GPS track recording system) there are three main parts: start recording, record GPS positioning, end recording and storage, as shown on the right side of the figure above. In practical application, take the navigation system as an example :(1) start navi, recording work related configuration; (2) Receiving the GPSLocation record of the onLocationChanged callback of Android system; (3) At the end of the navigation (Stop navi), stop recording and save the file.
Related code display
Relevant variables used
private LocationManager mLocationManager; // System locationManager private LocationListener; Private Boolean mIsRecording = false; Private List<String> mGpsList; Private String mRecordFileName; // GPS file nameCopy the code
- Start recording
Start recording is generally at the beginning of the whole system. For example, in the navigation scenario, when “Start navigation”, you can start the configuration of “startRecordLocation”
Public void startRecordLocation(Context Context, String fileName) {// Not recording if (mIsRecording) {return; } Toast.makeText(context, "start record location..." , Toast.LENGTH_SHORT).show(); // Initialize locationManager and locationListener mLocationManager = (locationManager) context.getSystemService(Context.LOCATION_SERVICE); mLocationListener = new MyLocationListener(); Try {/ / add the listener mLocationManager. RequestLocationUpdates (LocationManager GPS_PROVIDER, 0, 0, mLocationListener); } catch (SecurityException e) { Toast.makeText(context, "start record location error!!!" , Toast.LENGTH_SHORT).show(); Log.e(TAG, "startRecordLocation Exception", e); e.printStackTrace(); } mRecordFileName = fileName; if (! mRecordFileName.endsWith(".gps")) { mRecordFileName += ".gps"; } mIsRecording = true; }Copy the code
- Record track during recording
Recording location is usually done by calling “recordGPSLocation” when you get the Android onLocationChanged callback.
public void recordGPSLocation(Location location) { if (mIsRecording && location ! = null) {// Record location to list mGpsList. Add (locationToString(location)); }}Copy the code
LocationToString tool method
GPS track points that drive navigation generally include the following elements, such as longitude, latitude, accuracy, Angle, speed, time and altitude, so they are recorded here to prepare for track playback in the later stage.
private String locationToString(Location location) { StringBuilder sb = new StringBuilder(); long time = System.currentTimeMillis(); String timeStr = gpsDataFormatter.format(new Date(time)); sb.append(location.getLatitude()); sb.append(","); sb.append(location.getLongitude()); sb.append(","); sb.append(location.getAccuracy()); sb.append(","); sb.append(location.getBearing()); sb.append(","); sb.append(location.getSpeed()); sb.append(","); sb.append(timeStr); sb.append(","); Sb. Append (df) format (1000.0) (double) time /); / / sb. Append (df) format (System. CurrentTimeMillis () / 1000.0)); / / sb. Append (df) format (the location. The getTime () / 1000.0)); sb.append(","); sb.append(location.getAltitude()); sb.append("\n"); return sb.toString(); }Copy the code
- End recording and save the GPS file
End recording is generally used at the end of the entire system. For example, in the navigation scene, stop recording when “end navigation” calls “stopRecordLocation”.
public void stopRecordLocation(Context context) { Toast.makeText(context, "stop record location, save to file..." , Toast.LENGTH_SHORT).show(); / / remove the listener mLocationManager. RemoveUpdates (mLocationListener); String storagePath = StorageUtil.getStoragePath(context); String filePath = storagePath + mRecordFileName; // storagePath String filePath = storagePath + mRecordFileName; saveGPS(filePath); mIsRecording = false; }Copy the code
GPS track storage tool method
private void saveGPS(String path) {
OutputStreamWriter writer = null;
try {
File outFile = new File(path);
File parent = outFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
OutputStream out = new FileOutputStream(outFile);
writer = new OutputStreamWriter(out);
for (String line : mGpsList) {
writer.write(line);
}
} catch (Exception e) {
Log.e(TAG, "saveGPS Exception", e);
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to flush output stream", e);
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to close output stream", e);
}
}
}
}
Copy the code
The getStoragePath tool method of StorageUtil
/ / stored in the follow path/TencentMapSDK/navigation under private static final String NAVIGATION_PATH = "/ TencentMapSDK/navigation"; Public static String getStoragePath(Context Context) {if (Context == null) {return null; } String strFolder; boolean hasSdcard; try { hasSdcard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } catch (Exception e) { Log.e(TAG, "getStoragePath Exception", e); e.printStackTrace(); hasSdcard = false; } if (! hasSdcard) { strFolder = context.getFilesDir().getPath() + NAVIGATION_PATH; File file = new File(strFolder); if (! file.exists()) { file.mkdirs(); } } else { strFolder = Environment.getExternalStorageDirectory().getPath() + NAVIGATION_PATH; File file = new File(strFolder); if (! File.exists ()) {// Directory does not exist, create directory if (! file.mkdirs()) { strFolder = context.getFilesDir().getPath() + NAVIGATION_PATH; file = new File(strFolder); if (! file.exists()) { file.mkdirs(); Try {String newFile = strFolder + "/.test"; File tmpFile = new File(newFile); if (tmpFile.createNewFile()) { tmpFile.delete(); } } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "getStoragePath Exception", e); strFolder = context.getFilesDir().getPath() + NAVIGATION_PATH; file = new File(strFolder); if (! file.exists()) { file.mkdirs(); } } } } return strFolder; }Copy the code
The results show
It ends up in the navigation directory under the phone’s directory
The follow-up work
Later, the recorded GPS files can be explained to share track playback in navigation scenarios
Author: Tencent Location Service
Link: my.oschina.net/u/4209404/b…
Source: Open Source China
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.