Android 获取GPS速度
2017-01-30 10:19
513 查看
初始化:
onDestroy时remove监听:
位置监听:
// 位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
// location.getAltitude(); -- 海拔
updateSpeedByLocation(location);
}
/**
* GPS状态变化时触发
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE: // GPS状态为可见时
MyLog.i("GPS", "当前GPS状态为可见状态");
break;
case LocationProvider.OUT_OF_SERVICE: // GPS状态为服务区外时
MyLog.i("GPS", "当前GPS状态为服务区外状态");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE: // GPS状态为暂停服务时
MyLog.i("GPS", "当前GPS状态为暂停服务状态");
break;
}
}
/**
* GPS开启时触发
*/
public void onProviderEnabled(String provider) {
Location location = locationManager.getLastKnownLocation(provider);
updateSpeedByLocation(location);
}
/**
* GPS禁用时触发
*/
public void onProviderDisabled(String provider) {
// updateView(null);
}
};
获取查询条件:
/**
* 返回查询条件
*
* @return
*/
private Criteria getLocationCriteria() {
Criteria criteria = new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true); // 设置是否要求速度
criteria.setCostAllowed(false); // 设置是否允许运营商收费
criteria.setBearingRequired(false); // 设置是否需要方位信息
criteria.setAltitudeRequired(false); // 设置是否需要海拔信息
criteria.setPowerRequirement(Criteria.POWER_LOW); // 设置对电源的需求
return criteria;
}
GPS状态监听
// 状态监听
GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_FIRST_FIX: // 第一次定位
MyLog.i("GPS", "GPS_EVENT_FIRST_FIX");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS: // 卫星状态改变
GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 获取当前状态
int maxSatellites = gpsStatus.getMaxSatellites(); // 获取卫星颗数的默认最大值
Iterator<GpsSatellite> iters = gpsStatus.getSatellites()
.iterator(); // 创建一个迭代器保存所有卫星
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
GpsSatellite s = iters.next();
count++;
}
MyLog.i("GPS", "Satellite Number:" + count);
break;
case GpsStatus.GPS_EVENT_STARTED: // 定位启动
MyLog.i("GPS", "GPS_EVENT_STARTED");
break;
case GpsStatus.GPS_EVENT_STOPPED: // 定位结束
MyLog.i("GPS", "GPS_EVENT_STOPPED");
break;
}
};
};
根据Location获取速度
private void updateSpeedByLocation(Location location) {
int tempSpeed = (int) (location.getSpeed() * 3.6); // m/s --> Km/h
adasSpeed = tempSpeed;
recordSpeed = tempSpeed;
nowLatitude = location.getLatitude();
nowLongitude = location.getLongitude();
MyLog.i("GPS", "Speed:" + tempSpeed);
if (recorderFront != null) {
if (recordSpeed > 0) {
recorderFront.setSpeed(recordSpeed);
recordSpeed = 0; // 清除速度
}
recorderFront.setLat(new DecimalFormat("#.00000")
.format(nowLatitude) + "");
recorderFront.setLong(new DecimalFormat("#.00000")
.format(nowLongitude) + "");
}
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 位置 if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { // TODO: Open GPS } else { String bestProvider = locationManager.getBestProvider( getLocationCriteria(), true); // 获取位置信息 // 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER Location location = locationManager .getLastKnownLocation(bestProvider); // 监听状态 locationManager.addGpsStatusListener(gpsStatusListener); // 绑定监听,有4个参数 // 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种 // 参数2,位置信息更新周期,单位毫秒 // 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息 // 参数4,监听 // 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 1秒更新一次,或最小位移变化超过1米更新一次; // 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置 locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 1, locationListener); }
onDestroy时remove监听:
@Override protected void onDestroy() { if (locationManager != null) { locationManager.removeGpsStatusListener(gpsStatusListener); } super.onDestroy(); }
位置监听:
// 位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
// location.getAltitude(); -- 海拔
updateSpeedByLocation(location);
}
/**
* GPS状态变化时触发
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE: // GPS状态为可见时
MyLog.i("GPS", "当前GPS状态为可见状态");
break;
case LocationProvider.OUT_OF_SERVICE: // GPS状态为服务区外时
MyLog.i("GPS", "当前GPS状态为服务区外状态");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE: // GPS状态为暂停服务时
MyLog.i("GPS", "当前GPS状态为暂停服务状态");
break;
}
}
/**
* GPS开启时触发
*/
public void onProviderEnabled(String provider) {
Location location = locationManager.getLastKnownLocation(provider);
updateSpeedByLocation(location);
}
/**
* GPS禁用时触发
*/
public void onProviderDisabled(String provider) {
// updateView(null);
}
};
获取查询条件:
/**
* 返回查询条件
*
* @return
*/
private Criteria getLocationCriteria() {
Criteria criteria = new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true); // 设置是否要求速度
criteria.setCostAllowed(false); // 设置是否允许运营商收费
criteria.setBearingRequired(false); // 设置是否需要方位信息
criteria.setAltitudeRequired(false); // 设置是否需要海拔信息
criteria.setPowerRequirement(Criteria.POWER_LOW); // 设置对电源的需求
return criteria;
}
GPS状态监听
// 状态监听
GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_FIRST_FIX: // 第一次定位
MyLog.i("GPS", "GPS_EVENT_FIRST_FIX");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS: // 卫星状态改变
GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 获取当前状态
int maxSatellites = gpsStatus.getMaxSatellites(); // 获取卫星颗数的默认最大值
Iterator<GpsSatellite> iters = gpsStatus.getSatellites()
.iterator(); // 创建一个迭代器保存所有卫星
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
GpsSatellite s = iters.next();
count++;
}
MyLog.i("GPS", "Satellite Number:" + count);
break;
case GpsStatus.GPS_EVENT_STARTED: // 定位启动
MyLog.i("GPS", "GPS_EVENT_STARTED");
break;
case GpsStatus.GPS_EVENT_STOPPED: // 定位结束
MyLog.i("GPS", "GPS_EVENT_STOPPED");
break;
}
};
};
根据Location获取速度
private void updateSpeedByLocation(Location location) {
int tempSpeed = (int) (location.getSpeed() * 3.6); // m/s --> Km/h
adasSpeed = tempSpeed;
recordSpeed = tempSpeed;
nowLatitude = location.getLatitude();
nowLongitude = location.getLongitude();
MyLog.i("GPS", "Speed:" + tempSpeed);
if (recorderFront != null) {
if (recordSpeed > 0) {
recorderFront.setSpeed(recordSpeed);
recordSpeed = 0; // 清除速度
}
recorderFront.setLat(new DecimalFormat("#.00000")
.format(nowLatitude) + "");
recorderFront.setLong(new DecimalFormat("#.00000")
.format(nowLongitude) + "");
}
}
相关文章推荐
- Android 使用SVG动画
- Android编程之通知栏的用法小结
- Android编程实现调用系统图库与裁剪图片功能
- Android开发入门——推箱子游戏开发实战(十三)
- Android中bindService的细节之四:bindService时,你所忽略的代码流程细节
- 关于Android Matrix pre post 的理解
- Mac 连不上华为 p9 处理历程(一)
- Mac 连不上华为 p9 处理历程(二)
- Android中事件处理机制之——View的事件分发详解(一)
- Android轮播广告
- 专制各种升级失败:纯手工刷入Android L步骤,100%成功!
- Android 工具类 -->文件压缩
- SharedSDK(一):Android SharedSDK使用步骤
- Android进阶之路 - IntentService的使用方式
- Android平台美颜相机/Camera实时滤镜/视频编解码/影像后期/人脸技术探索——1.2 GPGPU解决方案简述
- Android 如何收集已发布程序的崩溃信息
- 优雅的实现多类型列表的Adapter
- [OpenGL]从零开始写一个Android平台下的全景视频播放器——5.8 使用MediaPlayer播放在线视频
- 底部导航BottomNavigationBar的使用
- android_104_Animation