首页 > 代码库 > GPS相关知识
GPS相关知识
1 public class Main extends Activity implements OnClickListener { 2 private LocationManager locationManager; 3 private Location location; 4 private Criteria criteria; 5 private String provider; 6 7 private TextView tv_Latitude;// 纬度 8 private TextView tv_Longitude;// 经度 9 private TextView tv_High;// 海拔 10 private TextView tv_Direction;// 方向 11 private TextView tv_Speed;// 速度 12 private TextView tv_GpsTime;// 获取的时间 13 private TextView tv_InfoType;// 信息的来源 14 private EditText et_SetTimeSpace;// 设置时间间隔 15 16 private Button manual_btn; 17 private Button settimespace_btn; 18 private Button exit_btn; 19 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 setTitle("GPS"); 24 // 隐藏输入法 25 getWindow().setSoftInputMode( 26 WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 27 initView(); 28 initLocation(); 29 } 30 31 // 初始化 32 @SuppressWarnings("static-access") 33 private void initLocation() { 34 // 初始化locationManager:获得系统所提供的location_service 35 locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE); 36 /** 37 * LocationManager.GPS_PROVIDER 38 * 39 * 精度比较高,但是慢而且消耗电力, 而且可能因为天气原因或者障碍物而无法获取卫星信息,另外设备可能没有GPS模块; 40 * LocationManager.NETWORK_PROVIDER 41 * 42 * 通过网络获取定位信息,精度低,耗电少,获取信息速度较快,不依赖GPS模块。 43 */ 44 if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 45 /* 注意:criteria有多种设置 */ 46 47 // 第一种:criteria 48 criteria = new Criteria(); 49 criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度 50 criteria.setAltitudeRequired(true);// 显示海拔 51 criteria.setBearingRequired(true);// 显示方向 52 criteria.setSpeedRequired(true);// 显示速度 53 criteria.setCostAllowed(false);// 不允许有花费 54 criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗 55 56 // 第二种:criteria 57 // criteria = new Criteria(); 58 // criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度 59 // criteria.setAltitudeRequired(false);//不要求海拔信息 60 // criteria.setBearingRequired(false);//不要求方位信息 61 // criteria.setCostAllowed(true);//是否允许付费 62 // criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求 63 64 provider = locationManager.getBestProvider(criteria, true); 65 66 locationManager.requestLocationUpdates(provider, 5000, 10, 67 locationListener);// 位置变化监听,默认5秒一次,距离10米以上 68 } else 69 showInfo(null, -1); 70 } 71 72 /** 73 * 获取GPS相应的数据 74 * 75 * @return 76 */ 77 private GPSModel getLastPosition() { 78 GPSModel gpsModel = new GPSModel(); 79 location = locationManager.getLastKnownLocation(provider); 80 if (location != null) { 81 gpsModel.Latitude = (double) (location.getLatitude()); 82 gpsModel.Longitude = (double) (location.getLongitude()); 83 gpsModel.High = location.getAltitude(); 84 gpsModel.Direct = location.getBearing(); 85 gpsModel.Speed = location.getSpeed(); 86 87 Date d = new Date(); 88 d.setTime(location.getTime()); 89 gpsModel.GpsTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", d) 90 .toString(); 91 d = null; 92 } 93 return gpsModel; 94 } 95 96 // 显示信息 97 private void showInfo(GPSModel gpsModel, int infotype) { 98 if (gpsModel == null) { 99 if (infotype == -1) {100 tv_Latitude.setText("GPS功能已关闭");101 tv_Longitude.setText("");102 tv_High.setText("");103 tv_Direction.setText("");104 tv_Speed.setText("");105 tv_GpsTime.setText("");106 tv_InfoType.setText("");107 manual_btn.setEnabled(false);108 settimespace_btn.setEnabled(false);109 et_SetTimeSpace.setEnabled(false);110 }111 } else {112 double y = gpsModel.Latitude;// 纬度113 double x = gpsModel.Longitude;// 经度114 tv_Latitude.setText("纬度:y=" + String.valueOf(y));115 tv_Longitude.setText("经度:x=" + String.valueOf(x));116 tv_High.setText(String.format("海拔:%f", gpsModel.High));117 tv_Direction.setText(String.format("方向:%f", gpsModel.Direct));118 tv_Speed.setText(String.format("速度:%f", gpsModel.Speed));119 tv_GpsTime.setText(String.format("GPS时间:%s", gpsModel.GpsTime));120 121 gpsModel.InfoType = infotype;122 switch (infotype) {123 case 1:124 tv_InfoType.setText("信息来源状态:手动获取更新");125 break;126 case 2:127 tv_InfoType.setText("信息来源状态:位置改变更新");128 break;129 }130 }131 132 }133 134 @SuppressWarnings("static-access")135 @Override136 public void onClick(View v) {137 if (v.equals(manual_btn)) {138 showInfo(getLastPosition(), 1);139 }140 if (v.equals(settimespace_btn)) {141 if (TextUtils.isEmpty(et_SetTimeSpace.getText().toString())) {142 Toast.makeText(this, "请输入更新时间间隔", Toast.LENGTH_LONG).show();143 et_SetTimeSpace.requestFocus();144 return;145 }146 147 int timespace = Integer.valueOf(et_SetTimeSpace.getText()148 .toString()) * 1000;149 if (locationManager.isProviderEnabled(locationManager.GPS_PROVIDER))150 locationManager.requestLocationUpdates(provider, timespace, 10,151 locationListener);152 }153 if (v.equals(exit_btn))154 android.os.Process.killProcess(android.os.Process.myPid());155 }156 157 private final LocationListener locationListener = new LocationListener() {158 159 @Override160 public void onLocationChanged(Location arg0) {161 showInfo(getLastPosition(), 2);162 }163 164 @Override165 public void onProviderDisabled(String arg0) {166 showInfo(null, -1);167 }168 169 @Override170 public void onProviderEnabled(String arg0) {171 }172 173 @Override174 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {175 }176 177 };178 179 private void initView() {180 tv_Latitude = (TextView) findViewById(R.id.tvlatitude);181 tv_Longitude = (TextView) findViewById(R.id.tvlongitude);182 tv_High = (TextView) findViewById(R.id.tvhigh);183 tv_Direction = (TextView) findViewById(R.id.tvdirection);184 tv_Speed = (TextView) findViewById(R.id.tvspeed);185 tv_GpsTime = (TextView) findViewById(R.id.tvgpstime);186 tv_InfoType = (TextView) findViewById(R.id.tvinfotype);187 et_SetTimeSpace = (EditText) findViewById(R.id.ettimespace);188 189 manual_btn = (Button) findViewById(R.id.btnmanual);190 manual_btn.setOnClickListener(this);191 settimespace_btn = (Button) findViewById(R.id.btnsettimespace);192 settimespace_btn.setOnClickListener(this);193 exit_btn = (Button) findViewById(R.id.btnexit);194 exit_btn.setOnClickListener(this);195 }196 197 }
GPS相关知识
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。