android--GPS定位

来源:互联网 发布:latex mac版 编辑:程序博客网 时间:2024/04/29 21:20

LocationManager 类

该类供访问系统的位置服务。
该类不能直接实例化,必须通过Context.getSystemService(Context.LOCATION_SERVICE)获取该类对象。
所有位置API方法需要ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限。

Public Methods:
boolean isProviderEnabled(String provider)—–返回当前启用/禁用状态给定的提供者。
String getBestProvider(Criteria criteria, boolean enabledOnly)–根据标准返回定位信息。
Location getLastKnownLocation(String provider)–返回一个位置指示。
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) 请求位置更新方法。

Criteria类

设置获取位置的标准。如精度,耗电等。

android 定位一般有四种方法,这四种方式分别是:
GPS定位,WIFI定准,基站定位,AGPS定位


Android GPS

优点

  1. 通过GPS方式准确度是最高的。
  2. GPS走的是卫星通信的通道,在没有网络连接的情况下也能用。

缺点

1. 比较耗电;2. 绝大部分用户默认不开启GPS模块;3. 从GPS模块启动到获取第一次定位数据,可能需要比较长的时间;4. 室内几乎无法使用。

使用方法

  1. 需要如下权限:

    android.permission.ACCESS_FINE_LOCATION

2.代码

private voidgetLocation()          {            // 获取位置管理服务            LocationManager locationManager;            String serviceName = Context.LOCATION_SERVICE;            locationManager = (LocationManager)this.getSystemService(serviceName);            // 查找到服务信息            Criteria criteria = new Criteria();           criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度            criteria.setAltitudeRequired(false);            criteria.setBearingRequired(false);            criteria.setCostAllowed(true);           criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗            String provider =locationManager.getBestProvider(criteria, true); // 获取GPS信息            Location location =locationManager.getLastKnownLocation(provider); // 通过GPS获取位置            updateToNewLocation(location);            // 设置监听*器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米            locationManager.requestLocationUpdates(provider,100 * 1000, 500,                locationListener);                  }

获取经纬度

private voidupdateToNewLocation(Location location) {            TextView tv1;            tv1 = (TextView)this.findViewById(R.id.tv1);            if (location != null) {              double latitude = location.getLatitude();              double longitude=location.getLongitude();              tv1.setText( 维度: + latitude+ \n经度 +longitude);            } else {              tv1.setText( 无法获取地理信息 );            }          }

Android 基站定位

Android Wifi定位

据一个固定的WifiMAC地址,通过收集到的该Wifi热点的位置,然后访问网络上的定位服务以获得经纬度坐标。因为它和基站定位其实都需要使用网络,所以在Android也统称为Network方式。

public classWiFiInfoManager implements Serializable {          private static final long serialVersionUID= -4582739827003032383L;          private Context context;          public WiFiInfoManager(Context context) {            super();            this.context = context;          }          public WifiInfo getWifiInfo() {            WifiManager manager = (WifiManager)context               .getSystemService(Context.WIFI_SERVICE);            WifiInfo info = new WifiInfo();            info.mac =manager.getConnectionInfo().getBSSID();            Log.i( TAG , WIFI MACis: + info.mac);            return info;          }          public class WifiInfo {            public String mac;            public WifiInfo() {              super();            }          }        }

下面是把地址发送给google服务器,代码如下

public staticLocation getWIFILocation(WifiInfo wifi) {            if (wifi == null) {              Log.i( TAG , wifiis null. );              return null;            }            DefaultHttpClient client = newDefaultHttpClient();            HttpPost post = new HttpPost( http://www.google.com/loc/json );            JSONObject holder = new JSONObject();            try {              holder.put( version , 1.1.0 );              holder.put( host , maps.google.com );              JSONObject data;              JSONArray array = new JSONArray();              if (wifi.mac != null  wifi.mac.trim().length()  0) {                data = new JSONObject();               data.put( mac_address , wifi.mac);               data.put( signal_strength , 8);                data.put( age , 0);                array.put(data);              }              holder.put( wifi_towers ,array);              Log.i( TAG , request json: + holder.toString());              StringEntity se = newStringEntity(holder.toString());              post.setEntity(se);              HttpResponse resp =client.execute(post);              int state =resp.getStatusLine().getStatusCode();              if (state == HttpStatus.SC_OK) {                HttpEntity entity =resp.getEntity();                if (entity != null) {                  BufferedReader br = newBufferedReader(                      newInputStreamReader(entity.getContent()));                  StringBuffer sb = newStringBuffer();                  String resute = ;                  while ((resute =br.readLine()) != null) {                    sb.append(resute);                  }                  br.close();                  Log.i( TAG , response json: + sb.toString());                  data = newJSONObject(sb.toString());                  data = (JSONObject)data.get( location );                  Location loc = newLocation(                     android.location.LocationManager.NETWORK_PROVIDER);                  loc.setLatitude((Double)data.get( latitude ));                  loc.setLongitude((Double)data.get( longitude ));                 loc.setAccuracy(Float.parseFloat(data.get( accuracy )                      .toString()));                  loc.setTime(System.currentTimeMillis());                  return loc;                } else {                  return null;                }              } else {                Log.v( TAG , state + );                return null;              }            } catch (Exception e) {              Log.e( TAG ,e.getMessage());              return null;            }          }

AGPS定位

AGPS(AssistedGPS:辅助全球卫星定位系统)是结合GSM或GPRS与传统卫星定位。和纯GPS、基地台三角定位比较,AGPS能提供范围更广、更省电、速度更快的定位服务。AGPS技术是一种结合了网络基站信息和GPS信息对移动台进行定位的技术,可以在GSM/GPRS、WCDMA和CDMA2000网络中使进行用。该技术需要在手机内增加GPS接收机模块,并改造手机的天线,同时要在移动网络上加建位置服务器、差分GPS基准站等设备。
AGPS解决方案的优势主要体现在其定位精度上,在室外等空旷地区,其精度在正常的GPS工作环境下,可以达到10米左右,堪称目前定位精度最高的一种定位技术。
该技术的另一优点为:首次捕获GPS信号的时间一般仅需几秒,不像GPS的首次捕获时间可能要2~3分钟。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿眼里有泪水怎么办 十个月宝宝拉肚子怎么办 腹泻十几天了怎么办 新生儿37.3度要怎么办 婴儿发烧37.3度怎么办 小孩子太吵了怎么办 一年级孩子学习差怎么办 马克笔涂错了怎么办 孩子说话不流利怎么办 儿童最近不吃饭怎么办 不爱吃饭偏瘦怎么办 一岁半小宝宝不爱吃饭怎么办 家养的小鸡打架怎么办 小鸡关在一起打架怎么办 小孩感冒出汗多怎么办 宝宝感冒出汗了怎么办 出汗多怎么办%3f 孩子睡觉出汗多怎么办 三年级孩子成绩下滑怎么办 幼儿数学不开窍怎么办 孩子一年级学习跟不上怎么办 孩子学习不认真怎么办 孩子平时不细心怎么办? 脖子出现黑圈怎么办 简历留白太多怎么办 excel数字外面加个圈怎么办 电脑输入法数字数不上怎么办 大班教案迷路了怎么办 迷路了怎么办活动反思 头突然眩晕是怎么办 孩子学习拖拉懒惰怎么办 头发晕想睡觉怎么办 五年级错别字多怎么办 入党志愿书写错字怎么办 高中不会写作文怎么办 考试不会写作文怎么办 高考不会写作文怎么办 孩子总是写错字怎么办 小孩总是写错字怎么办 孩子马虎不认真怎么办 小孩计算老出错怎么办