设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1530|回复: 0
打印 上一主题 下一主题

Android GPS的文档 [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-9-24 08:52:39 |只看该作者 |倒序浏览
一、简单的用户定位概念:(得到用户位置)User location 作用:
       1、获取用户位置
       2、追踪用户的移动

       User location的关键API:
       Location manger:管理android用户定位服务
       Location provider:提供多种定位方式

       定位实现步骤:
       1、在manifest中注册,注册实现一个大致精确的定位。
       2、获取locationmanager对象,所有的定位服务必须,以该对象作为基础
       3、选择一个locationprovider,有两种方式,一种是GPS,一种是network
       4、绑定一个locationListener,这个监听器,当手机位置发生改变就会触发函数,函数

       具体实现:以下部分只得到当前的经纬度

       1、获得locationmanger对象
        LocationManagerlocationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);

       2、定义监听器实现implements LocationListener的方法,实现其默认的方法,在onLocationChanged(Locationlocation)方法里面将处理每次位置发生改变的我们的处理

       3、选择locationmanger和绑定监听器,第一个参数选择使用GPS方式的定位服务,当然也有network方式的定位,第二个参数指定多少时间触发,第三个参数指定多少距离触发,这个两个值都只是简单的索引而已,并不代表真实的值,真实的值,可能大于或者小于该值,第四个参数绑定监听器。

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, newTestLocationListener());

       二、获取最佳的locationprovider的概念:

        以下概念基于获得了locationmanger对象
        1、获取设备上的所有的provider List<String> providers = locationManager.getAllProviders();//迭代查看有哪些provider,有三种,GPS,network, passive,主要用的都是前两。for (Iterator iterator = providers.iterator();iterator.hasNext();) {String provider =(String) iterator.next();System.out.println(provider);}

        2、获取最佳的provider,最适合我们的provider,是相对于我们的需要而定义的Criteria:设置一系列的查询条件,查询符合条件的provider。

         具体事例:
  1. Criteria criteria = new Criteria();//设置查询条件
  2. criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
  3. criteria.setPowerRequirement(Criteria.POWER_LOW);//消耗低电量
  4. criteria.setAltitudeRequired(false);//不需要海拔
  5. criteria.setSpeedRequired(false);//不需要速度信息
  6. criteria.setCostAllowed(false);//不产生费用

  7. //把criteria看成是一组条件,交由locationmanager查询,第二个参数为不管查询出来的provider是否可用都查询出来
  8. String provider = locationManager.getBestProvider(criteria,false);
  9. System.out.println("best provider ----> " + provider);
复制代码
总共有10个条件,具体可见文档,部分如下:

void   setAccuracy(int accuracy)设置精度,Indicates the desired accuracy for latitude and  longitude.
   void   setAltitudeRequired(boolean altitudeRequired)是否需要提供海拔信息Indicates whether the provider must provide altitude  information.
   void   setBearingAccuracy(int accuracy)获取方向的精度Indicates the desired bearing accuracy.
   void   setBearingRequired(boolean bearingRequired) 与上面的是配对的,是否需要Indicates whether the provider must provide bearing  information.
   void   setCostAllowed(boolean costAllowed) 得到方位信息的时候是否会产生费用Indicates whether the provider is allowed to incur  monetary cost.
   void   setHorizontalAccuracy(int accuracy) 设置水平方向的精度Indicates the desired horizontal accuracy (latitude and  longitude).
   void   setPowerRequirement(int level)设置电量消耗的级别Indicates the desired maximum power level.
   void   setSpeedAccuracy(int accuracy)设置是否需要速度Indicates the desired speed accuracy.
   void   setSpeedRequired(boolean speedRequired)…Indicates whether the provider must provide speed  information.
   void   setVerticalAccuracy(int accuracy)设置垂直方面的精度Indicates the desired vertical accuracy (altitude).

       3、追踪用户位置定位
       主要用到的是前面讲到的绑定监听器的方法
  1. public void requestLocationUpdates (Stringprovider, long minTime, float minDistance, LocationListener listener)
复制代码
里面的第二和第三这两个参数,一个是两次更新用户位置的最小时间,单位是毫秒,也就是说,获取一次位置后最少过多久再获取一次;另外一个也相似,指更新的最小距离,单位是米。

       将第一部分的绑定代码换成如下就可以:
  1. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5000, newTestLocationListener());
复制代码
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0
您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-9 17:26 , Processed in 1.065107 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部