ITeye博客

GPS

AndLi · 2011-11-22 20:10 · GPS · 移动开发 · Markdown

 

public class Main extends Activity implements OnClickListener {
	private LocationManager locationManager;
	private Location location;
	private Criteria criteria;
	private String provider;

	private TextView tv_Latitude;// 纬度
	private TextView tv_Longitude;// 经度
	private TextView tv_High;// 海拔
	private TextView tv_Direction;// 方向
	private TextView tv_Speed;// 速度
	private TextView tv_GpsTime;// 获取的时间
	private TextView tv_InfoType;// 信息的来源
	private EditText et_SetTimeSpace;// 设置时间间隔

	private Button manual_btn;
	private Button settimespace_btn;
	private Button exit_btn;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTitle("GPS");
		// 隐藏输入法
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		initView();
		initLocation();
	}

	// 初始化
	@SuppressWarnings("static-access")
	private void initLocation() {
		// 初始化locationManager:获得系统所提供的location_service
		locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
		/**
		 * LocationManager.GPS_PROVIDER
		 * 
		 * 精度比较高,但是慢而且消耗电力, 而且可能因为天气原因或者障碍物而无法获取卫星信息,另外设备可能没有GPS模块;
		 * LocationManager.NETWORK_PROVIDER
		 * 
		 * 通过网络获取定位信息,精度低,耗电少,获取信息速度较快,不依赖GPS模块。
		 */
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			/* 注意:criteria有多种设置 */

			// 第一种:criteria
			criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
			criteria.setAltitudeRequired(true);// 显示海拔
			criteria.setBearingRequired(true);// 显示方向
			criteria.setSpeedRequired(true);// 显示速度
			criteria.setCostAllowed(false);// 不允许有花费
			criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗

			// 第二种:criteria
			// criteria = new Criteria();
			// criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
			// criteria.setAltitudeRequired(false);//不要求海拔信息
			// criteria.setBearingRequired(false);//不要求方位信息
			// criteria.setCostAllowed(true);//是否允许付费
			// criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求

			provider = locationManager.getBestProvider(criteria, true);

			locationManager.requestLocationUpdates(provider, 5000, 10,
					locationListener);// 位置变化监听,默认5秒一次,距离10米以上
		} else
			showInfo(null, -1);
	}

	/**
	 * 获取GPS相应的数据
	 * 
	 * @return
	 */
	private GPSModel getLastPosition() {
		GPSModel gpsModel = new GPSModel();
		location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			gpsModel.Latitude = (double) (location.getLatitude());
			gpsModel.Longitude = (double) (location.getLongitude());
			gpsModel.High = location.getAltitude();
			gpsModel.Direct = location.getBearing();
			gpsModel.Speed = location.getSpeed();

			Date d = new Date();
			d.setTime(location.getTime());
			gpsModel.GpsTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", d)
					.toString();
			d = null;
		}
		return gpsModel;
	}

	// 显示信息
	private void showInfo(GPSModel gpsModel, int infotype) {
		if (gpsModel == null) {
			if (infotype == -1) {
				tv_Latitude.setText("GPS功能已关闭");
				tv_Longitude.setText("");
				tv_High.setText("");
				tv_Direction.setText("");
				tv_Speed.setText("");
				tv_GpsTime.setText("");
				tv_InfoType.setText("");
				manual_btn.setEnabled(false);
				settimespace_btn.setEnabled(false);
				et_SetTimeSpace.setEnabled(false);
			}
		} else {
			double y = gpsModel.Latitude;// 纬度
			double x = gpsModel.Longitude;// 经度
			tv_Latitude.setText("纬度:y=" + String.valueOf(y));
			tv_Longitude.setText("经度:x=" + String.valueOf(x));
			tv_High.setText(String.format("海拔:%f", gpsModel.High));
			tv_Direction.setText(String.format("方向:%f", gpsModel.Direct));
			tv_Speed.setText(String.format("速度:%f", gpsModel.Speed));
			tv_GpsTime.setText(String.format("GPS时间:%s", gpsModel.GpsTime));

			gpsModel.InfoType = infotype;
			switch (infotype) {
			case 1:
				tv_InfoType.setText("信息来源状态:手动获取更新");
				break;
			case 2:
				tv_InfoType.setText("信息来源状态:位置改变更新");
				break;
			}
		}

	}

	@SuppressWarnings("static-access")
	@Override
	public void onClick(View v) {
		if (v.equals(manual_btn)) {
			showInfo(getLastPosition(), 1);
		}
		if (v.equals(settimespace_btn)) {
			if (TextUtils.isEmpty(et_SetTimeSpace.getText().toString())) {
				Toast.makeText(this, "请输入更新时间间隔", Toast.LENGTH_LONG).show();
				et_SetTimeSpace.requestFocus();
				return;
			}

			int timespace = Integer.valueOf(et_SetTimeSpace.getText()
					.toString()) * 1000;
			if (locationManager.isProviderEnabled(locationManager.GPS_PROVIDER))
				locationManager.requestLocationUpdates(provider, timespace, 10,
						locationListener);
		}
		if (v.equals(exit_btn))
			android.os.Process.killProcess(android.os.Process.myPid());
	}

	private final LocationListener locationListener = new LocationListener() {

		@Override
		public void onLocationChanged(Location arg0) {
			showInfo(getLastPosition(), 2);
		}

		@Override
		public void onProviderDisabled(String arg0) {
			showInfo(null, -1);
		}

		@Override
		public void onProviderEnabled(String arg0) {
		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		}

	};

	private void initView() {
		tv_Latitude = (TextView) findViewById(R.id.tvlatitude);
		tv_Longitude = (TextView) findViewById(R.id.tvlongitude);
		tv_High = (TextView) findViewById(R.id.tvhigh);
		tv_Direction = (TextView) findViewById(R.id.tvdirection);
		tv_Speed = (TextView) findViewById(R.id.tvspeed);
		tv_GpsTime = (TextView) findViewById(R.id.tvgpstime);
		tv_InfoType = (TextView) findViewById(R.id.tvinfotype);
		et_SetTimeSpace = (EditText) findViewById(R.id.ettimespace);

		manual_btn = (Button) findViewById(R.id.btnmanual);
		manual_btn.setOnClickListener(this);
		settimespace_btn = (Button) findViewById(R.id.btnsettimespace);
		settimespace_btn.setOnClickListener(this);
		exit_btn = (Button) findViewById(R.id.btnexit);
		exit_btn.setOnClickListener(this);
	}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。