`

GPS

    博客分类:
  • GPS
 
阅读更多


 

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);
	}

}

 
 

  • 大小: 33.2 KB
  • GPS.zip (61.7 KB)
  • 下载次数: 23
分享到:
评论

相关推荐

    GPS仿真软件 gps模拟器 NMEA0183仿真软件

    【GPS模拟器(GPS仿真软件)Demo】 (Demo 仅用于演示。若有需求请购买正版。) 软件名称:NMEA Worker 软件版本:V1.2.00 简介: 1.NMEA Worker软件包括有GPS模拟器功能,可以按照NMEA0183标准模拟输出大部分GPS...

    GPS时间转换_MATLAB

    GPS时间转换程序,MATLAB版本。 gps2cal 将GPS周和周内秒转换到公历时间 gps2cal1 由公历日期和GPS周内秒计算公历时间 cal2gps 将公历GPS时间转换到GPS周和周内秒 mjd2cal 将简化儒略日转换到公历时间 cal2mjd 将...

    GPS校时程序(用手机上的GPS校对时间。实现精确对时)

    手机上使用GPS进行对时。对于个人而言GPS是最准确的对时方法。这个软件提供了这种对时方式。本软件为自由软件,自由复制,自由使用。 使用方法:在手机上直接运行软件即可。无需安装。运行的时候由于要搜星,所以...

    GPS模块GPS模块GPS模块

    GPS模块GPS模块GPS模块

    【提供操作视频】基于GPS+IMU的卡尔曼滤波融合定位算法仿真,其中惯导用来进行状态预测,GPS用来滤波矫正

    1.领域:matlab,GPS+IMU的卡尔曼滤波融合定位算法算法 2.内容:【提供操作视频】基于GPS+IMU的卡尔曼滤波融合定位算法仿真,其中惯导用来进行状态预测,GPS用来滤波矫正 3.用处:用于GPS+IMU的卡尔曼滤波融合定位...

    Arduino连接GPS 模块 NEO-6M读取定位数据

    Arduino连接GPS 模块 NEO-6M读取定位数据 GPS 卫星在精确的轨道上每天绕地球两次。每颗卫星都传输独特的信号和轨道参数,使 GPS 设备能够解码和计算卫星的精确位置。GPS 接收器使用此信息和三边测量来计算用户的确切...

    android使用GPS获取当前地理位置

    第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对 象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation有2方面功能:1. 获取当前地理...

    GPS协议校验和计算工具

    NMEA协议是为了在不同的GPS导航设备中建立统一的RTCM(海事无线电技术委员会)标准,它最初是由美国国家海洋电子协会(NMEA—The NationalMarine Electronics Association)制定的。 NMEA通讯协议所规定的通讯语句都已是...

    【高清版】《GPS软件接收机基础(第二版)》.zip

    GPS软件接收机基础》(第2版)采用软件接收机的观点,详细介绍了GPS接收机原理,涉及软件无线电和GPS两个热门领域。GPS接收机包括直接序列扩频信号接收技术和导航处理两个学科内容,涉及卫星星座及GPS信号的影响、...

    GPS_INS位置组合Matlab仿真源码

    GPS/INS位置组合输出校正Matlab仿真 文件说明: s_GPS_INS_position_sp_demo.m 组合主文件 kalman_GPS_INS_position_sp_NFb.m 卡尔曼滤波程序 ode500.mat 飞机飞行轨迹与INS输出数据 将三个文件放到同一个文件夹中...

    GPS RTK操作手册

    §1.1 GPS接收机与卫星信号 §1.2 GPS测量技术 §1.2.1 动态差分(RTK §1.2.2 伪距差分(DGPS §1.2.3 静态和快速静态 §1.3 GPS在测量工作中的应用 §1.3.1 控制测量 §1.3.2 地形测量 §1.3.3 放样 §1.4 RTK的...

    GPS数据转换X.Y坐标

    GPS系统接收数据坐标转换 GPS接收的数据往往是三维坐标,而在科学研究中我们通常用二维坐标。因此必须 进行坐标转换,下面我们介绍一种坐标转换,即把WGS84坐标转换为高斯—克吕 格坐标系。数字地图投影的...

    GPS信号仿真_gps的ca码和p码_GPS信号生成_gpsmatlabca码生成_GPS信号_gps仿真_

    生成GPS的CA码、P码,输入数据码长度,输出GPS信号仿真

    GPS 车载GPS GPS定位 物流GPS 苏州GPS

    GPS 车载GPS GPS定位 物流GPS 苏州GPS GPS车辆定位监控系统 物流车辆管理系统 苏州GPS服务中心

    GPS模拟仿真GPS卫星定位模拟仿真

    GPS模拟仿真GPS卫星定位模拟仿真 GPS卫星定位系统目前已经应用广泛,用51单片机模拟GPS发送机,用了SRAM,开启卫星接收软件,就可以看到时间,经度,纬度,速度等。不同的GPS格式解码的内容也不一样,有的有海拨高度...

    gps-matlab-code.zip_GPS信号生成_GPS宽带干扰_GPS干扰_单频信号 gps_卫星干扰

    gps信号的产生及其干扰的MATLAB仿真。包括卫星星座编程,c\a码生成,多普勒频移实现,gps信号生成,干扰噪声生成(宽带噪声,窄带噪声,单频噪声)

    全国小区数据,最全全国小区数据,全国小区gps,各地小区数据

    数据信息包括:小区名(name)、省份(province)、城市(city)、区域(area)、地址(address)、纬度(latitude)、经度(longitude)、纬度(GPS)(latitude_gps)、经度(GPS)(longitude_gps)、物业类型(type)、物业费(management...

    gpsapp5.16c

    9、对于Gpsapp软件本身就无法接收到GPS信号的情况,请检查Gpsapp目录是否生成gps.ini文件以及gps.ini文件中GPS端口及速率是否正确。 10、如发现WinCE系统手写笔压下并保持这一姿势来模拟鼠标右键点击失效,可将Gps...

    Gps协议解析,GPS格式

    Gps协议,Gps解析,Gps报文,GPS议格式 Gps协议,Gps解析,Gps报文,GPS议格式

    GPS-L1信号频谱_p码_gps频谱图_GPS_GPS信号_GPS-L1信号频谱

    GPS信号频谱,matlab语言,里面有CA码和P码的子文件,最后将各信号频谱画出来。

Global site tag (gtag.js) - Google Analytics