腾讯位置服务GPS轨迹录制-安卓篇
前言
在地图的使用中,尤其在导航场景下,进行GPS轨迹录制是十分必要并且有用的,本文会对于安卓系统下的轨迹录制部分做一个分享。
系统架构
对于一个GPSRecordSystem(GPS轨迹录制系统)主要分成3个部分:开始录制,录制GPS定位,结束录制并存储,如上图右方所示。在实际应用中,以导航系统为例:(1)在开始导航时(start navi),进行录制工作的相关配置;(2)收到安卓系统的onLocationChanged的callback进行GPSLocation的记录;(3)结束导航(stop navi)时,停止记录并存入文件。
相关代码展示
用到的相关变量
private LocationManager mLocationManager; // 系统locationManager
private LocationListener mLocationListener; // 系统locationListener
private boolean mIsRecording = false; // 是否正在录制
private List<String> mGpsList; // 记录gps的list
private String mRecordFileName; // gps文件名称
- 开始录制
开始录制一般是在整个系统工作之初,比如在导航场景下,当“开始导航”时,可以开始进行“startRecordLocation” 的配置
public void startRecordLocation(Context context, String fileName) {
// 已经在录制中不进行录制
if (mIsRecording) {
return;
}
Toast.makeText(context, "start record location...", Toast.LENGTH_SHORT).show();
// 初始化locationManager和locationListener
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
try {
// 添加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();
}
// 记录文件名称,笔者这里使用“realLocationRecord + routeID”形式进行记录
mRecordFileName = fileName;
if (!mRecordFileName.endsWith(".gps")) {
mRecordFileName += ".gps";
}
mIsRecording = true;
}
- 录制中记录轨迹 记录location一般是在获取安卓系统onLocationChanged回调时调用“recordGPSLocation”
public void recordGPSLocation(Location location) {
if (mIsRecording && location != null) {
// 记录location to list
mGpsList.add(locationToString(location));
}
}
locationToString工具方法
驱动导航工作的GPS轨迹点一般要包含以下几个要素,经度,纬度,精度,角度,速度,时间,海拔高度,所以在此记录下,为后期轨迹回放做准备。
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();
}
- 结束录制并保存gps文件
结束录制一般作用在整个系统的结尾,例如在导航场景下,“结束导航”时停止录制调用“stopRecordLocation”
public void stopRecordLocation(Context context) {
Toast.makeText(context, "stop record location, save to file...", Toast.LENGTH_SHORT).show();
// 移除listener
mLocationManager.removeUpdates(mLocationListener);
String storagePath = StorageUtil.getStoragePath(context); // 存储的路径
String filePath = storagePath + mRecordFileName;
saveGPS(filePath);
mIsRecording = false;
}
GPS轨迹存储工具方法
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);
}
}
}
}
StorageUtil的getStoragePath工具方法
// 存储在跟路径下/TencentMapSDK/navigation
private static final String NAVIGATION_PATH = "/tencentmapsdk/navigation";
// getStoragePath工具方法
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()) { // 目录不存在,创建目录
if (!file.mkdirs()) {
strFolder = context.getFilesDir().getPath() + NAVIGATION_PATH;
file = new File(strFolder);
if (!file.exists()) {
file.mkdirs();
}
}
} else { // 目录存在,创建文件测试是否有权限
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;
}
结果展示
最终存储在了手机目录下的navigation目录
后续工作
后续可以对于录制的gps文件讲解在导航场景下进行轨迹回放的分享

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
这个好用的分布式应用配置中心,我们把它开源了
导读:SpringBoot的时代到来,对于曾经面向一堆XML配置的开发经历,那真是一大福音,一切都变得非常简洁,留下的就是简化的配置文件设置。但在分布式环境下呢?众多的实例集群下,动态的实例迁移等情况时常发生,导致配置管理的工作变得复杂且困难,百度研发团队通过多年的架构建设经验,把过往的配置管理的相关经验沉淀成一套通用的解决方案,现以开源的方式回馈给社区开发者,希望帮助大家彻底解决配置建设的难题。 全文约4200字,预计阅读时间8分钟。 1 分布式环境下的配置管理挑战 可以说配置化是当今应用开发与部署必备的一个能力要求,我们通常把一些容易变化以及依赖外部情况而变化的内容,通过配置化的方式来实现,这样我们就可以在零编码的情况下实现功能调整,实现极低成本的应用扩展能力。而对于配置管理方式,最为常见的方式就是配置文件方式, 通过特定的文件内容格式进行设置, 部署时也会与应用程序放在一起。这样使用方式在单机情况下比较容易且简单,但是在大型的分布式应用场景下,特别又要区分不同环境(开发,测试,线上等)就会导致管理成本与出错风险急速加大。 以上述场景为例,涉及的问题与挑战有: 操作复杂,成本高:如...
-
下一篇
Serverless:这真的是未来吗?(一)
原文 | https://www.pulumi.com/blog/is_serverless_the_future_part_1/ 作者 | Lee Briggs & Piers Karsenbarg 译者 | donghui 许多开发人员说,无服务器是计算的未来,而其他开发人员说,它永远不会成功。我们自己的观点没有那么两极分化。我们将无服务器视为一种选择,这是从初创企业到中型企业,再到大型企业的一个可能的垫脚石。在这两篇博文中,我们将讨论无服务器如何适应这一过程,以及它的优点和缺点。 我们的目标是帮助您切实地评估无服务器计算。我们希望激发讨论,而不是下意识的反应,无论是赞成还是反对。希望这些博客文章能帮助您在所有相关人员中展开讨论,就最佳业务方案达成一致。该课程可能涉及无服务器,也可能不涉及。在这第一篇文章中,我们将考虑在讨论无服务器时最常见的几个问题。在第二篇文章中,我们将研究一些更广泛的问题。 什么是无服务器? “无服务器"这个术语有点用词不当。更愤世嫉俗的人可能会嘀咕,“无服务器仍然在服务器上运行!“这是真的。不管你使用什么云提供商,你总是使用服务器来运行你的应用程序。...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2全家桶,快速入门学习开发网站教程
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- MySQL数据库在高并发下的优化方案
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果