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监听:java
@Override protected void onDestroy() { if (locationManager != null) { locationManager.removeGpsStatusListener(gpsStatusListener); } super.onDestroy(); }
位置监听:git
// 位置监听 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); } };
获取查询条件:ide
/** * 返回查询条件 * * @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状态监听ui
// 状态监听 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获取速度code
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) + ""); } }