百度定位SDK实现获取当前经纬度及位置

15 篇文章 0 订阅

使用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API中的定位SDK,可以一次性获取当前位置经纬度以及详细地址信息,还可以获取周边POI信息,同时可以设定位置通知点,当到达某一位置时,发出通知信息等方式来告知用户。jar包下载以及官方文档请参照:百度定位SDK,前提是需要注册百度开发者账号。

下面来看看定位的基本原理,目前,定位SDK可以通过GPS、基站、Wifi信号进行定位。基本定位流程如下图所示,当应用程序向定位SDK发起定位请求时,定位SDK会根据当前的GPS、基站、Wifi信息生成相对应的定位依据。然后定位SDK会根据定位依据来进行定位。如果需要,定位SDK会向定位服务器发送网络请求。定位服务器会根据请求的定位依据推算出对应的坐标位置,然后根据用户的定制信息,生成定位结果返回给定位SDK。

                     

到官方下载jar文件后添加到工程,工程目录截图如下:


注意要把locSDK_2.4.jar添加到当天工程,右键jar文件-Build path-Add to。。。


上代码

布局文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_start"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginTop="20dp"  
  12.         android:text="Start"/>  
  13.   
  14.     <TextView  
  15.         android:id="@+id/tv_loc_info"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:textSize="18sp" />  
  19.   
  20. </LinearLayout>  

配置文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.ericssonlabs"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >  
  10.     </uses-permission>  
  11.     <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >  
  12.     </uses-permission>  
  13.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >  
  14.     </uses-permission>  
  15.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >  
  16.     </uses-permission>  
  17.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >  
  18.     </uses-permission>  
  19.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >  
  20.     </uses-permission>  
  21.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >  
  22.     </uses-permission>  
  23.     <uses-permission android:name="android.permission.READ_PHONE_STATE" >  
  24.     </uses-permission>  
  25.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >  
  26.     </uses-permission>  
  27.     <uses-permission android:name="android.permission.INTERNET" />  
  28.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >  
  29.     </uses-permission>  
  30.     <uses-permission android:name="android.permission.READ_LOGS" >  
  31.     </uses-permission>  
  32.   
  33.     <application  
  34.         android:icon="@drawable/ic_launcher"  
  35.         android:label="@string/app_name" >  
  36.         <activity  
  37.             android:name=".LocationDemoActivity"  
  38.             android:label="@string/app_name" >  
  39.             <intent-filter>  
  40.                 <action android:name="android.intent.action.MAIN" />  
  41.   
  42.                 <category android:name="android.intent.category.LAUNCHER" />  
  43.             </intent-filter>  
  44.         </activity>  
  45.   
  46.         <service  
  47.             android:name="com.baidu.location.f"  
  48.             android:enabled="true"  
  49.             android:permission="android.permission.BAIDU_LOCATION_SERVICE"  
  50.             android:process=":remote" >  
  51.             <intent-filter>  
  52.                 <action android:name="com.baidu.location.service_v2.4" />  
  53.             </intent-filter>  
  54.         </service>  
  55.     </application>  
  56.   
  57. </manifest>  

实现代码:

[java] view plaincopy
  1. public class LocationDemoActivity extends Activity {  
  2.     private TextView locationInfoTextView = null;  
  3.     private Button startButton = null;  
  4.     private LocationClient locationClient = null;  
  5.     private static final int UPDATE_TIME = 5000;  
  6.     private static int LOCATION_COUTNS = 0;  
  7.       
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.           
  13.         locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info);  
  14.         startButton = (Button) this.findViewById(R.id.btn_start);  
  15.           
  16.           
  17.         locationClient = new LocationClient(this);  
  18.         //设置定位条件  
  19.         LocationClientOption option = new LocationClientOption();  
  20.         option.setOpenGps(true);                                //是否打开GPS  
  21.         option.setCoorType("bd09ll");                           //设置返回值的坐标类型。  
  22.         option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级  
  23.         option.setProdName("LocationDemo");                     //设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。  
  24.         option.setScanSpan(UPDATE_TIME);                        //设置定时定位的时间间隔。单位毫秒  
  25.         locationClient.setLocOption(option);  
  26.           
  27.         //注册位置监听器  
  28.         locationClient.registerLocationListener(new BDLocationListener() {  
  29.               
  30.             @Override  
  31.             public void onReceiveLocation(BDLocation location) {  
  32.                 // TODO Auto-generated method stub  
  33.                 if (location == null) {  
  34.                     return;  
  35.                 }  
  36.                 StringBuffer sb = new StringBuffer(256);  
  37.                 sb.append("Time : ");  
  38.                 sb.append(location.getTime());  
  39.                 sb.append("\nError code : ");  
  40.                 sb.append(location.getLocType());  
  41.                 sb.append("\nLatitude : ");  
  42.                 sb.append(location.getLatitude());  
  43.                 sb.append("\nLontitude : ");  
  44.                 sb.append(location.getLongitude());  
  45.                 sb.append("\nRadius : ");  
  46.                 sb.append(location.getRadius());  
  47.                 if (location.getLocType() == BDLocation.TypeGpsLocation){  
  48.                     sb.append("\nSpeed : ");  
  49.                     sb.append(location.getSpeed());  
  50.                     sb.append("\nSatellite : ");  
  51.                     sb.append(location.getSatelliteNumber());  
  52.                 } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){  
  53.                     sb.append("\nAddress : ");  
  54.                     sb.append(location.getAddrStr());  
  55.                 }  
  56.                 LOCATION_COUTNS ++;  
  57.                 sb.append("\n检查位置更新次数:");  
  58.                 sb.append(String.valueOf(LOCATION_COUTNS));  
  59.                 locationInfoTextView.setText(sb.toString());  
  60.             }  
  61.               
  62.             @Override  
  63.             public void onReceivePoi(BDLocation location) {  
  64.             }  
  65.               
  66.         });  
  67.           
  68.         startButton.setOnClickListener(new OnClickListener() {  
  69.               
  70.             @Override  
  71.             public void onClick(View v) {  
  72.                 if (locationClient == null) {  
  73.                     return;  
  74.                 }  
  75.                 if (locationClient.isStarted()) {  
  76.                     startButton.setText("Start");  
  77.                     locationClient.stop();  
  78.                 }else {  
  79.                     startButton.setText("Stop");  
  80.                     locationClient.start();  
  81.                     /* 
  82.                      *当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。 
  83.                      *调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。 
  84.                      *如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求, 
  85.                      *返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。 
  86.                      *定时定位时,调用一次requestLocation,会定时监听到定位结果。 
  87.                      */  
  88.                     locationClient.requestLocation();  
  89.                 }  
  90.             }  
  91.         });  
  92.           
  93.     }  
  94.       
  95.     @Override  
  96.     protected void onDestroy() {  
  97.         super.onDestroy();  
  98.         if (locationClient != null && locationClient.isStarted()) {  
  99.             locationClient.stop();  
  100.             locationClient = null;  
  101.         }  
  102.     }  
  103.       
  104.       
  105. }  

来看看最后实现效果,点击Start后进入位置监听状态,根据设置的监听时间间隔进行定位,如果位置有变化则进行位置更新,同时显示了检测位置更新的次数,如果开启了GPS,则获取到卫星后,进行GPS定位:



设置位置提醒的功能我这里就没实现了,感兴趣的可以参考开发指南

加入我们的QQ群或微信公众账号请查看:Ryan's zone公众账号及QQ群


欢迎关注我的新浪微博和我交流:@唐韧_Ryan

原文地址:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值