android gps开发必备资料
来源:互联网 发布:有淘宝就能贷款的软件 编辑:程序博客网 时间:2024/05/17 23:36
入门资料参考:
How accurate is Android GPS? Part 1: Understanding Location Data
How accurate is Android GPS? Part 2 – Consuming real-time locations
Google Developer docs – Location Strategies
Android blog – Deep dive into location
GPS Testing Tool (open source)
HTML5 Geolocation API – How accurate is it, really?
测试demo工程:
GpsActivty.java
- public class GpsActivity extends Activity {
- private EditText editText;
- private TextView logText;
- private LocationManager lm;
- private static final String TAG="GpsActivity";
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- lm.removeUpdates(locationListener);
- }
- private void setLog(String txt){
- printTime();
- setLogInfo(txt);
- }
- private void setLogInfo(String txt){
- logText.setText(txt+"\n"+"--------------------\n"+logText.getText());
- }
- private void printTime(){
- Calendar ca = Calendar.getInstance();
- int year = ca.get(Calendar.YEAR);//获取年份
- int month=ca.get(Calendar.MONTH);//获取月份
- int day=ca.get(Calendar.DATE);//获取日
- int minute=ca.get(Calendar.MINUTE);//分
- int hour=ca.get(Calendar.HOUR);//小时
- int second=ca.get(Calendar.SECOND);//秒
- int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
- setLogInfo("当前日期:" + year +"年"+ month +"月"+ day + "日");
- setLogInfo(">>>" + hour +"时"+ minute +"分"+ second +"秒");
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gps_demo);
- editText=(EditText)findViewById(R.id.editText);
- logText=(TextView) this.findViewById(R.id.logText);
- lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- //判断GPS是否正常启动
- if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
- Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
- setLog("请开启GPS导航...");
- //返回开启GPS导航设置界面
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivityForResult(intent,0);
- return;
- }
- //为获取地理位置信息时设置查询条件
- String bestProvider = lm.getBestProvider(getCriteria(), true);
- //获取位置信息
- //如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
- Location location= lm.getLastKnownLocation(bestProvider);
- updateView(location);
- //监听状态
- lm.addGpsStatusListener(listener);
- //绑定监听,有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(),更新位置
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
- }
- //位置监听
- private LocationListener locationListener=new LocationListener() {
- /**
- * 位置信息变化时触发
- */
- public void onLocationChanged(Location location) {
- updateView(location);
- Log.i(TAG, "时间:"+location.getTime());
- Log.i(TAG, "经度:"+location.getLongitude());
- Log.i(TAG, "纬度:"+location.getLatitude());
- Log.i(TAG, "海拔:"+location.getAltitude());
- setLog( "时间:"+location.getTime());
- setLog( "经度:"+location.getLongitude());
- setLog( "纬度:"+location.getLatitude());
- setLog( "海拔:"+location.getAltitude());
- }
- /**
- * GPS状态变化时触发
- */
- public void onStatusChanged(String provider, int status, Bundle extras) {
- switch (status) {
- //GPS状态为可见时
- case LocationProvider.AVAILABLE:
- Log.i(TAG, "当前GPS状态为可见状态");
- setLog("当前GPS状态为可见状态");
- break;
- //GPS状态为服务区外时
- case LocationProvider.OUT_OF_SERVICE:
- Log.i(TAG, "当前GPS状态为服务区外状态");
- setLog("当前GPS状态为服务区外状态");
- break;
- //GPS状态为暂停服务时
- case LocationProvider.TEMPORARILY_UNAVAILABLE:
- Log.i(TAG, "当前GPS状态为暂停服务状态");
- setLog("当前GPS状态为暂停服务状态");
- break;
- }
- }
- /**
- * GPS开启时触发
- */
- public void onProviderEnabled(String provider) {
- Location location=lm.getLastKnownLocation(provider);
- updateView(location);
- }
- /**
- * GPS禁用时触发
- */
- public void onProviderDisabled(String provider) {
- updateView(null);
- }
- };
- //状态监听
- GpsStatus.Listener listener = new GpsStatus.Listener() {
- public void onGpsStatusChanged(int event) {
- switch (event) {
- //第一次定位
- case GpsStatus.GPS_EVENT_FIRST_FIX:
- Log.i(TAG, "第一次定位");
- setLog("第一次定位");
- break;
- //卫星状态改变
- case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
- Log.i(TAG, "卫星状态改变");
- setLog("卫星状态改变");
- //获取当前状态
- GpsStatus gpsStatus=lm.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++;
- }
- System.out.println("搜索到:"+count+"颗卫星");
- setLog("搜索到:"+count+"颗卫星");
- break;
- //定位启动
- case GpsStatus.GPS_EVENT_STARTED:
- Log.i(TAG, "定位启动");
- setLog("定位启动");
- break;
- //定位结束
- case GpsStatus.GPS_EVENT_STOPPED:
- Log.i(TAG, "定位结束");
- setLog("定位结束");
- break;
- }
- };
- };
- /**
- * 实时更新文本内容
- *
- * @param location
- */
- private void updateView(Location location){
- if(location!=null){
- editText.setText("设备位置信息\n\n经度:");
- editText.append(String.valueOf(location.getLongitude()));
- editText.append("\n纬度:");
- editText.append(String.valueOf(location.getLatitude()));
- }else{
- //清空EditText对象
- editText.getEditableText().clear();
- }
- }
- /**
- * 返回查询条件
- * @return
- */
- private Criteria getCriteria(){
- Criteria criteria=new Criteria();
- //设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- //设置是否要求速度
- criteria.setSpeedRequired(false);
- // 设置是否允许运营商收费
- criteria.setCostAllowed(false);
- //设置是否需要方位信息
- criteria.setBearingRequired(false);
- //设置是否需要海拔信息
- criteria.setAltitudeRequired(false);
- // 设置对电源的需求
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- return criteria;
- }
- }
然后在layout目录创建 gps_demo.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:cursorVisible="false"
- android:editable="false"
- android:id="@+id/editText"/>
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:minHeight="300dp"
- android:id="@+id/logText"/>
- </LinearLayout>
- </ScrollView>
最后在AndroidManifest.xml里添加需要的权限
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
手机运行的效果图
0 0
- android gps开发必备资料
- android gps开发必备资料(含测试demo下载)
- droid gps开发必备资料(含测试demo下载) .
- Andriod开发必备资料
- Onvif 开发必备资料
- android手机gps相关资料
- Android开发必备资料之50例源码汇总
- 必备的游戏开发资料
- 必备的游戏开发资料
- 编程开发学习资料必备
- android GPS开发
- Android系统Gps开发
- Android开发---GPS定位
- Android GPS应用开发
- [资料]学习必备嵌入式开发板-4412开发板ARM+Android+linux技术全覆盖
- java Web开发必备资料整理
- java Web开发必备资料整理
- 2012java开发工程师必备精品资料
- 智能指针用法
- DelegatingRequestProcessor作用
- iOS利用AFNetworking(AFN) 实现图片上传
- eclipse配置maven环境简记
- 嵌入式裸机编程,对内存管理的一些见解
- android gps开发必备资料
- MySQL中用户权限、库权限、表权限的控制
- 启动计算机(来自现代操作系统第三版)
- Oracle笔记——DDL
- java解惑之try catch finally
- CommonAdapter-万能的ListView.GridView的适配器
- Android移动view动画问题 关于view的位置移动了,但view里面绑定的监听器还在原位
- Android Make脚本简记
- Git分支 - 创建,切换