In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to record the map GPS track. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
In the use of maps, especially in navigation scenes, it is necessary and useful to record GPS tracks.
System architecture
For a GPSRecordSystem (GPS track recording system), it is mainly divided into three parts: start recording, record GPS positioning, end recording and storage, as shown on the right side of the image above. In practical application, take the navigation system as an example: (1) configure the recording work at the beginning of the navigation (start navi); (2) receive the callback of the Android onLocationChanged to record the GPSLocation; (3) stop recording and save the file at the end of the navigation (stop navi).
Related code display
Relevant variables used
Private LocationManager mLocationManager; / / system locationManager private LocationListener mLocationListener; / / system locationListener private boolean mIsRecording = false; / / whether you are recording private List mGpsList; / / record the list private String mRecordFileName; / / gps file name of gps
Start recording
Starting recording is usually at the beginning of the work of the whole system, for example, in a navigation scene, when "start navigation", you can start to configure "startRecordLocation".
Public void startRecordLocation (Context context, String fileName) {/ / has not recorded 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 listener mLocationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0,0, mLocationListener) } catch (SecurityException e) {Toast.makeText (context, "start record location errorproof!", Toast.LENGTH_SHORT) .show (); Log.e (TAG, "startRecordLocation Exception", e); e.printStackTrace () } / / record file name. The author uses the form "realLocationRecord + routeID" to record mRecordFileName = fileName; if (! mRecordFileName.endsWith (".gps")) {mRecordFileName + = ".gps";} mIsRecording = true;}
Track recording location during recording usually calls "recordGPSLocation" when getting Android onLocationChanged callback.
Public void recordGPSLocation (Location location) {if (mIsRecording & & location! = null) {/ / record location to list mGpsList.add (locationToString (location));}}
LocationToString tool method
The GPS track points that drive the navigation work generally include the following elements, longitude, latitude, accuracy, angle, speed, time, altitude, so in this record, prepare for later track playback.
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 ((double) time / 1000.0)); / / sb.append (df.format (System.currentTimeMillis () / 1000.0)); / / sb.append (df.format (location.getTime () / 1000.0)) Sb.append (","); sb.append (location.getAltitude ()); sb.append ("\ n"); return sb.toString ();}
End recording and save the gps file
Ending recording usually works at the end of the entire system, for example, in a navigation scene, stop recording and call "stopRecordLocation" when "ending navigation"
Public void stopRecordLocation (Context context) {Toast.makeText (context, "stop record location, save to file...", Toast.LENGTH_SHORT). Show (); / / remove listener mLocationManager.removeUpdates (mLocationListener); String storagePath = StorageUtil.getStoragePath (context); / / path to storage String filePath = storagePath + mRecordFileName; saveGPS (filePath) MIsRecording = false;}
The method of GPS track Storage tool
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);}}
GetStoragePath tool method of StorageUtil
/ / stored in the following path / TencentMapSDK/navigation private static final String NAVIGATION_PATH = "/ tencentmapsdk/navigation"; / / getStoragePath tool method 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 a directory if (! file.mkdirs ()) {strFolder = context.getFilesDir () .getPath () + NAVIGATION_PATH; file = new File (strFolder) If (! file.exists ()) {file.mkdirs ();} else {/ / directory exists. Create a file to test whether try {String newFile = strFolder + "/ .test" File tmpFile = newFile (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;} result display
Finally, it is stored in the navigation directory under the mobile phone directory.
You can share the track playback in the navigation scene for the recorded gps file.
The above is the editor for you to share how to record the map GPS track, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 298
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.