Android三种定位方式的实现

移动开发
Android三种定位方式的实现,分别是通过GPS、百度定位、高德定位这三种常见的方式。

源码简介

Android三种定位方式的实现,GPS、百度定位、高德定位。

源码运行截图

代码片段:

  1. public class MainActivity extends Activity implements OnClickListener{ 
  2.   
  3.     private TextView mTextView; 
  4.     private Button gpsBtn, baiduBtn, amapBtn; 
  5.       
  6.     //gps 
  7.     private LocationManager gpsManager; 
  8.     //baidu 
  9.     private LocationClient baduduManager; 
  10.     //amap 
  11.     private LocationManagerProxy aMapManager; 
  12.   
  13.     @Override 
  14.     protected void onCreate(Bundle savedInstanceState) { 
  15.         super.onCreate(savedInstanceState); 
  16.         setContentView(R.layout.activity_main); 
  17.         mTextView = (TextView) findViewById(R.id.text); 
  18.         gpsBtn = (Button) findViewById(R.id.gps); 
  19.         baiduBtn = (Button) findViewById(R.id.baidu); 
  20.         amapBtn = (Button) findViewById(R.id.amap); 
  21.           
  22.         gpsBtn.setOnClickListener(this); 
  23.         baiduBtn.setOnClickListener(this); 
  24.         amapBtn.setOnClickListener(this); 
  25.     } 
  26.       
  27.     @Override 
  28.     public void onClick(View v) { 
  29.         switch (v.getId()) { 
  30.         case R.id.gps: 
  31.             if (gpsBtn.getText().toString().equals("开启GPS定位")) { 
  32.                 startGps(); 
  33.                 gpsBtn.setText("停止GPS定位"); 
  34.             } else { 
  35.                 stopGps(); 
  36.                 gpsBtn.setText("开启GPS定位"); 
  37.             } 
  38.             break
  39.         case R.id.baidu: 
  40.             if (baiduBtn.getText().toString().equals("开启百度定位")) { 
  41.                 startBaidu(); 
  42.                 baiduBtn.setText("停止百度定位"); 
  43.             } else { 
  44.                 stopBaidu(); 
  45.                 baiduBtn.setText("开启百度定位"); 
  46.             } 
  47.             break
  48.         case R.id.amap: 
  49.             if (amapBtn.getText().toString().equals("开启高德定位")) { 
  50.                 startAmap(); 
  51.                 amapBtn.setText("停止高德定位"); 
  52.             } else { 
  53.                 stopAmap(); 
  54.                 amapBtn.setText("开启高德定位"); 
  55.             } 
  56.             break
  57.   
  58.         default
  59.             break
  60.         } 
  61.     } 
  62.   
  63.     private void startAmap() { 
  64.         aMapManager = LocationManagerProxy.getInstance(this); 
  65.         /* 
  66.          * mAMapLocManager.setGpsEnable(false); 
  67.          * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location 
  68.          * API定位采用GPS和网络混合定位方式 
  69.          * ,***个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 
  70.          */ 
  71.         aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 200010, mAMapLocationListener); 
  72.     } 
  73.   
  74.     private void stopAmap() { 
  75.         if (aMapManager != null) { 
  76.             aMapManager.removeUpdates(mAMapLocationListener); 
  77.             aMapManager.destory(); 
  78.         } 
  79.         aMapManager = null
  80.     } 
  81.       
  82.     private void startBaidu() { 
  83.         if (baduduManager == null) { 
  84.             baduduManager = new LocationClient(this); 
  85.             //定位的配置 
  86.             LocationClientOption option = new LocationClientOption(); 
  87.             //定位模式选择,高精度、省电、仅设备 
  88.             option.setLocationMode(LocationMode.Hight_Accuracy);  
  89.             //定位坐标系类型选取, gcj02、bd09ll、bd09 
  90.             option.setCoorType("gcj02");  
  91.             //定位时间间隔 
  92.             option.setScanSpan(1000); 
  93.             //选择定位到地址 
  94.             option.setIsNeedAddress(true); 
  95.             baduduManager.setLocOption(option); 
  96.             //注册定位的成功的回调 
  97.             baduduManager.registerLocationListener(mBdLocationListener); 
  98.         } 
  99.         baduduManager.start(); 
  100.     } 
  101.       
  102.     private void stopBaidu() { 
  103.         baduduManager.stop(); 
  104.     } 
  105.       
  106.   
  107.     private void startGps() { 
  108.         // 获取到LocationManager对象 
  109.         gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
  110.           
  111.         //provider可为gps定位,也可为为基站和WIFI定位 
  112.         String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName(); 
  113.           
  114.         //3000ms为定位的间隔时间,10m为距离变化阀值,gpsListener为回调接口 
  115.         gpsManager.requestLocationUpdates(provider, 300010, gpsListener); 
  116.     } 
  117.       
  118.     private void stopGps() { 
  119.         gpsManager.removeUpdates(gpsListener); 
  120.     } 
  121.   
  122.     // 创建位置监听器 
  123.     private LocationListener gpsListener = new LocationListener() { 
  124.           
  125.         // 位置发生改变时调用 
  126.         @Override 
  127.         public void onLocationChanged(Location location) { 
  128.             Log.e("Location""onLocationChanged"); 
  129.             double latitude = location.getLatitude(); 
  130.             double longitude = location.getLongitude(); 
  131.             float speed = location.getSpeed(); 
  132.             long time = location.getTime(); 
  133.             String s = "latitude--->" + latitude 
  134.                     +  "  longitude--->" + longitude 
  135.                     +  "  speed--->" + speed  
  136.                     +  "  time--->" + new Date(time).toLocaleString(); 
  137.             mTextView.setText("GPS定位\n" + s); 
  138.         } 
  139.   
  140.         // provider失效时调用 
  141.         @Override 
  142.         public void onProviderDisabled(String provider) { 
  143.             Log.e("Location""onProviderDisabled"); 
  144.         } 
  145.   
  146.         // provider启用时调用 
  147.         @Override 
  148.         public void onProviderEnabled(String provider) { 
  149.             Log.e("Location""onProviderEnabled"); 
  150.         } 
  151.   
  152.         // 状态改变时调用 
  153.         @Override 
  154.         public void onStatusChanged(String provider, int status, Bundle extras) { 
  155.             Log.e("Location""onStatusChanged"); 
  156.         } 
  157.     }; 
  158.       
  159.     private BDLocationListener mBdLocationListener = new BDLocationListener() { 
  160.           
  161.         @Override 
  162.         public void onReceiveLocation(BDLocation location) { 
  163.             //Receive Location  
  164.             StringBuffer sb = new StringBuffer(256); 
  165.             sb.append("time : "); 
  166.             sb.append(location.getTime()); 
  167.             sb.append("\nerror code : "); 
  168.             sb.append(location.getLocType()); 
  169.             sb.append("\nlatitude : "); 
  170.             sb.append(location.getLatitude()); 
  171.             sb.append("\nlontitude : "); 
  172.             sb.append(location.getLongitude()); 
  173.             sb.append("\nradius : "); 
  174.             sb.append(location.getRadius()); 
  175.             if (location.getLocType() == BDLocation.TypeGpsLocation){ 
  176.                 sb.append("\nspeed : "); 
  177.                 sb.append(location.getSpeed()); 
  178.                 sb.append("\nsatellite : "); 
  179.                 sb.append(location.getSatelliteNumber()); 
  180.                 sb.append("\ndirection : "); 
  181.                 sb.append("\naddr : "); 
  182.                 sb.append(location.getAddrStr()); 
  183.                 sb.append(location.getDirection()); 
  184.             } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ 
  185.                 sb.append("\naddr : "); 
  186.                 sb.append(location.getAddrStr()); 
  187.                 sb.append("\noperationers : "); 
  188.                 sb.append(location.getOperators()); 
  189.             } 
  190.             mTextView.setText("百度定位\n" + sb.toString()); 
  191.         } 
  192.     }; 
  193.       
  194.     private AMapLocationListener mAMapLocationListener = new AMapLocationListener() { 
  195.           
  196.         @Override 
  197.         public void onStatusChanged(String provider, int status, Bundle extras) { 
  198.               
  199.         } 
  200.           
  201.         @Override 
  202.         public void onProviderEnabled(String provider) { 
  203.               
  204.         } 
  205.           
  206.         @Override 
  207.         public void onProviderDisabled(String provider) { 
  208.               
  209.         } 
  210.           
  211.         @Override 
  212.         public void onLocationChanged(Location location) { 
  213.               
  214.         } 
  215.           
  216.         @Override 
  217.         public void onLocationChanged(AMapLocation location) { 
  218.             if (location != null) { 
  219.                 Double geoLat = location.getLatitude(); 
  220.                 Double geoLng = location.getLongitude(); 
  221.                 String cityCode = ""
  222.                 String desc = ""
  223.                 Bundle locBundle = location.getExtras(); 
  224.                 if (locBundle != null) { 
  225.                     cityCode = locBundle.getString("citycode"); 
  226.                     desc = locBundle.getString("desc"); 
  227.                 } 
  228.                 String str = ("定位成功:(" + geoLng + "," + geoLat + ")" 
  229.                         + "\n精    度    :" + location.getAccuracy() + "米" 
  230.                         + "\n定位方式:" + location.getProvider() + "\n定位时间:" 
  231.                         + new Date(location.getTime()).toLocaleString() + "\n城市编码:" 
  232.                         + cityCode + "\n位置描述:" + desc + "\n省:" 
  233.                         + location.getProvince() + "\n市:" + location.getCity() 
  234.                         + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location 
  235.                         .getAdCode()); 
  236.                 mTextView.setText("高德定位\n" + str); 
  237.             } 
  238.         } 
  239.     }; 
  240.   
源码链接:http://down.51cto.com/data/1968757
责任编辑:chenqingxiang 来源: 网络整理
相关推荐

2021-11-05 21:33:28

Redis数据高并发

2021-06-24 08:52:19

单点登录代码前端

2018-04-02 14:29:18

Java多线程方式

2023-12-04 09:31:13

CSS卡片

2010-03-12 17:52:35

Python输入方式

2022-11-03 08:44:24

代理模式Java设计模式

2010-09-07 16:31:27

CSS

2021-03-07 10:17:40

RDMA网络传输网络协议

2019-11-20 18:52:24

物联网智能照明智能恒温器

2012-07-17 09:16:16

SpringSSH

2022-01-20 08:38:02

Java接口Lambda

2013-01-04 15:47:54

Android开发平铺UI设计

2010-08-03 09:20:33

Flex读取XML配置

2023-08-22 07:05:34

PowerShellWindows

2011-07-25 12:41:38

接入方式布线

2017-07-14 15:07:23

2010-08-24 09:43:33

2009-07-29 09:36:07

无线通信接入方式

2021-11-26 11:07:14

cowsay命令Linux

2010-09-13 12:19:03

点赞
收藏

51CTO技术栈公众号