当前位置: 移动技术网 > 移动技术>移动开发>Android > android 定位的4种方式介绍

android 定位的4种方式介绍

2019年07月24日  | 移动技术网移动技术  | 我要评论
android 定位一般有四种方法,这四种方式分别是:gps定位,wifi定准,基站定位,agps定位,      &n

android 定位一般有四种方法,这四种方式分别是:gps定位,wifi定准,基站定位,agps定位,
                             
(1)android gps:需要gps硬件支持,直接和卫星交互来获取当前经纬度,这种方式需要手机支持gps模块(现在大部分的智能机应该都有了)。通过gps方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电;2,绝大部分用户默认不开启gps模块;3,从gps模块启动到获取第一次定位数据,可能需要比较长的时间;4,室内几乎无法使用。这其中,缺点2,3都是比较致命的。需要指出的是,gps走的是卫星通信的通道,在没有网络连接的情况下也能用。
                             
要实用adnroid平台的gps设备,首先需要添加上权限,所以需要添加如下权限:  
                             

uses-permission android:name= android.permission.access_fine_location  /uses-permission

具体实现代码如下:

首先判断gps模块是否存在或者是开启:

private voidopengpssettings() {
            locationmanager alm = (locationmanager)this
               .getsystemservice(context.location_service);
            if (alm
               .isproviderenabled(android.location.locationmanager.gps_provider)) {
              toast.maketext(this, gps模块正常 ,toast.length_short)
                  .show();
              return;
            } 
            toast.maketext(this, 请开启gps! ,toast.length_short).show();
            intent intent = newintent(settings.action_security_settings);
           startactivityforresult(intent,0); //此为设置完成后返回到获取界面
          }

如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到gps设置页面:
                            
获取代码如下:

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( 无法获取地理信息 );
            }
          }

(2)android 基站定位:android 基站定位只要明白了基站/wifi定位的原理,自己实现基站/wifi定位其实不难。基站定位一般有几种,第一种是利用手机附近的三个基站进行三角定位,由于每个基站的位置是固定的,利用电磁波在这三个基站间中转所需要时间来算出手机所在的坐标;第二种则是利用获取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信号强度,将这些数据发送到google的定位web服务里,就能拿到当前所在的位置信息,误差一般在几十米到几百米之内。其中信号强度这个数据很重要,
                           
               这里笔者就不多做解释了,直接给出一个文章,这个文章写的非常好,
                           
              
                             
(3)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();
            }
          }
        }

上面是取到wifi的mac地址的方法,下面是把地址发送给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;
            }
          }

(3.1)而wifi定位与基站定位的结合,笔者也在网上找到一个很好的文章,笔者对此就不做任何解释,直接给出网址:
                          
              
                            

4. agps定位

agps(assistedgps:辅助全球卫星定位系统)是结合gsm或gprs与传统卫星定位,利用基地台代送辅助卫星信息,以缩减gps芯片获取卫星信号的延迟时间,受遮盖的室内也能借基地台讯号弥补,减轻gps芯片对卫星的依赖度。和纯gps、基地台三角定位比较,agps能提供范围更广、更省电、速度更快的定位服务,理想误差范围在10公尺以内,日本和美国都已经成熟运用agps于lbs服务(location based service,基于位置的服务)。agps技术是一种结合了网络基站信息和gps信息对移动台进行定位的技术,可以在gsm/gprs、wcdma和cdma2000网络中使进行用。该技术需要在手机内增加gps接收机模块,并改造手机的天线,同时要在移动网络上加建位置服务器、差分gps基准站等设备。agps解决方案的优势主要体现在其定位精度上,在室外等空旷地区,其精度在正常的gps工作环境下,可以达到10米左右,堪称目前定位精度最高的一种定位技术。该技术的另一优点为:首次捕获gps信号的时间一般仅需几秒,不像gps的首次捕获时间可能要2~3分钟

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网