原文】 

开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助:

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服务里,就能拿到当前所在的位置信息,误差一般在几十米到几百米之内。其中信号强度这个数据很重要,
                            
               这里笔者就不多做解释了,直接给出一个文章,这个文章写的非常好,
                            
               http://www.jb51.net/article/34522.htm
                             
(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定位与基站定位的结合,笔者也在网上找到一个很好的文章,笔者对此就不做任何解释,直接给出网址:
                           
               http://www.jb51.net/article/52673.htm

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分钟

android 定位的四种方式的更多相关文章

  1. android 定位的几种方式介绍

    [地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...

  2. android:launchMode的四种方式

    Activity一共有以下四种launchMode: standard singleTop singleTask singleInstance 1.standard standard模式是默认的启动模 ...

  3. Android按钮绑定四种方式

    public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...

  4. Android 查看项目依赖树的四种方式

    Android 查看项目依赖树的四种方式: 方式一: ./gradlew 模块名:dependencies //查看单独模块的依赖 ./gradlew :app:dependencies --conf ...

  5. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览

    Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览   作为一个完成的应用程序,数据存储操作是必不可少的. ...

  6. Android异步更新UI的四种方式

    Android异步更新UI的四种方式 2015-09-06 09:23 segmentfault 字号:T | T 大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中 ...

  7. Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。

    原文:Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式. Android Activity 的四种启动模 ...

  8. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  9. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

    ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...

随机推荐

  1. 让/etc/profile文件修改后立即生效(转)

    方法1:让/etc/profile文件修改后立即生效 ,可以使用如下命令:# .  /etc/profile注意: . 和 /etc/profile 有空格方法2:让/etc/profile文件修改后 ...

  2. 2016"百度之星" - 初赛(Astar Round2A)

    题目链接: http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=701 1001 : 矩阵快速幂 #include <iostre ...

  3. 在CentOS 6上搭建私有的Docker Registry

    在CentOS 6上搭建私有的Docker Registry v2Registry概念 :Registry是一个无状态的, 高可扩展的服务器端应用程序, 用于存储和分发Docker Image. 依赖 ...

  4. sql 截取字符串与 截取字符串最长的字符串

    ); set @str='aa,32,22,55,7'; ) as '第一个逗号的索引值' )),),),'') as '第一个值' ),len(@str)) as '从第一逗号开始截取出后面的字符串 ...

  5. setTimeout()和setInterval() 何时被调用执行

    定义 setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔 ...

  6. Java集合的线程安全用法

    线程安全的集合包含2个问题 1.多线程并发修改一 个 集合 怎么办? 2.如果迭代的过程中 集合 被修改了怎么办? a.一个线程在迭代,另一个线程在修改 b.在同一个线程内用同一个迭代器对象进行迭代. ...

  7. CSS一些总结

    1. display block:块元素,默认宽度为100%,可以设置元素的宽高,默认占满一行.块元素包括div,h1-h6,form,table,ul,ol等: inline:行内元素,默认宽度为内 ...

  8. Python 安装mssql (Ubuntu)

    1. Python.h:没有那个文件或目录 apt-get install python-dev 2.sqlfront.h:没有那个文件或目录 apt-get install freetds-dev

  9. lucene 建立索引的过程

    时间 -- ::  CSDN博客 原文 http://blog.csdn.net/caohaicheng/article/details/ 看lucene主页(http://lucene.apach ...

  10. diff和patch配合使用(转载备用)

    Linux下diff与patch命令的配合使用 在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作. [A] diffdiff命令用于比较文件的差异,可以用于制作patch文件 ...