Android使用百度地图API实现定位app

前言:有时候,我们活得很累,并非生活过于刻薄,而是我们太容易被外界的氛围所感染,被他人的情绪所左右。行走在人群中,我们总是感觉有无数穿心掠肺的目光,有很多飞短流长的冷言,最终乱了心神,渐渐被缚于自己编织的一团乱麻中。

好消息是五月份终于要过去了······课实在太多了,上的头都大了······

转载请标明出处:http://blog.csdn.net/android_for_james/article/details/51533511

源码我放到了文章的末尾。

先来看看运行效果:

1.普通模式:                                       2.实时路况:                                       3.罗盘模式:

4.卫星模式:由于我的位置是旅顺,是敏感城市所以卫星图像百度不予以显示,在其他地方是可以正常显示的



一、申请秘钥

在使用百度地图SDK为您提供的各种LBS能力之前,您需要获取百度地图移动版的开发密钥,该密钥与您的百度账户相关联。因此,您必须先有百度帐户,才能获得开发密钥。并且,该密钥与您创建的过程名称有关。
Key的申请地址为:http://lbsyun.baidu.com/apiconsole/key

二、下载相关SDK:

1.百度地图SDK:
2.百度定位SDK:

三、环境配置

第一步:在工程app/libs目录下放入baidumapapi_vX_X_X.jar包,在src/main/目录下新建jniLibs目录,工程会自动加载src目录下的so动态库,放入libBaiduMapSDK_vX_X_X_X.so如下图所示,注意jar和so的前3位版本号必须一致,并且保证使用一次下载的文件夹中的两个文件,不能不同功能组件的jar或so交叉使用。

第二步:工程配置还需要把jar包集成到自己的工程中,放入libs目录下。对于每个jar文件,右键-选择Add As Library,导入到工程中。对应在build.gradle生成工程所依赖的jar文件说明,具体配置为下图:

第三步:
(1)在application中添加开发密钥和默认服务
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="html"><application>    
  2.     <meta-data    
  3.         android:name="com.baidu.lbsapi.API_KEY"    
  4.         android:value="开发者 key" />    
  5.         <service  
  6.             android:name="com.baidu.location.f"  
  7.             android:enabled="true"  
  8.             android:process=":remote">  
  9.         </service>  
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="html" style="color: rgb(98, 98, 98); font-size: 14px;"><pre name="code" class="html">    <com.baidu.mapapi.map.MapView  
  2.         android:id="@+id/id_bmapView"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="fill_parent"  
  5.         android:clickable="true" />  

</application>
 
 
 
 
 
 (2)添加所需权限 
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  2. <uses-permission android:name="android.permission.INTERNET"/>  
  3. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />  
  4. <uses-permission android:name="android.permission.WAKE_LOCK"/>  
  5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  6. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  7. <uses-permission android:name="android.permission.GET_TASKS" />  
  8. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  9. <uses-permission android:name="android.permission.WRITE_SETTINGS" />  

(3)布局文件中添加地图控件

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <com.baidu.mapapi.map.MapView  
  2.     android:id="@+id/id_bmapView"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:clickable="true" />  


四、实现结构:

1.MylocationListener负责定位操作
2.MyOrientationListener负责定位图标与方向传感器相结合

五、具体实现:

1.初始化 SDK引用的Context 全局变量:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     SDKInitializer.initialize(getApplicationContext());  
  5.     setContentView(R.layout.activity_main);  
  6. }  

2.生命周期的匹配:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected void onStop() {  
  3.     super.onStop();  
  4.     //停止定位  
  5.     mBaiduMap.setMyLocationEnabled(false);  
  6.     mlocationClient.stop();  
  7.     myOrientationListener.stop();  
  8. }  
  9.   
  10. @Override  
  11. protected void onResume() {  
  12.     super.onResume();  
  13.     mMapView.onResume();  
  14. }  
  15.   
  16. @Override  
  17. protected void onPause() {  
  18.     super.onPause();  
  19.     mMapView.onPause();  
  20. }  
  21.   
  22. @Override  
  23. protected void onDestroy() {  
  24.     super.onDestroy();  
  25.     mMapView.onDestroy();  
  26. }  
3.定位操作实现:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MylocationListener implements BDLocationListener  
  2.     {  
  3.         //定位请求回调接口  
  4.         private boolean isFirstIn=true;  
  5.         //定位请求回调函数,这里面会得到定位信息  
  6.         @Override  
  7.         public void onReceiveLocation(BDLocation bdLocation) {  
  8.             //BDLocation 回调的百度坐标类,内部封装了如经纬度、半径等属性信息  
  9.             //MyLocationData 定位数据,定位数据建造器  
  10.             /* 
  11.             * 可以通过BDLocation配置如下参数 
  12.             * 1.accuracy 定位精度 
  13.             * 2.latitude 百度纬度坐标 
  14.             * 3.longitude 百度经度坐标 
  15.             * 4.satellitesNum GPS定位时卫星数目 getSatelliteNumber() gps定位结果时,获取gps锁定用的卫星数 
  16.             * 5.speed GPS定位时速度 getSpeed()获取速度,仅gps定位结果时有速度信息,单位公里/小时,默认值0.0f 
  17.             * 6.direction GPS定位时方向角度 
  18.             * */  
  19.             mLatitude= bdLocation.getLatitude();  
  20.             mLongitude=bdLocation.getLongitude();  
  21.             MyLocationData data= new MyLocationData.Builder()  
  22.                     .direction(mCurrentX)//设定图标方向  
  23.                     .accuracy(bdLocation.getRadius())//getRadius 获取定位精度,默认值0.0f  
  24.                     .latitude(mLatitude)//百度纬度坐标  
  25.                     .longitude(mLongitude)//百度经度坐标  
  26.                     .build();  
  27.             //设置定位数据, 只有先允许定位图层后设置数据才会生效,参见 setMyLocationEnabled(boolean)  
  28.             mBaiduMap.setMyLocationData(data);  
  29.             //配置定位图层显示方式,三个参数的构造器  
  30.             /* 
  31.             * 1.定位图层显示模式 
  32.             * 2.是否允许显示方向信息 
  33.             * 3.用户自定义定位图标 
  34.             * 
  35.             * */  
  36.             MyLocationConfiguration configuration  
  37.                     =new MyLocationConfiguration(locationMode,true,mIconLocation);  
  38.             //设置定位图层配置信息,只有先允许定位图层后设置定位图层配置信息才会生效,参见 setMyLocationEnabled(boolean)  
  39.             mBaiduMap.setMyLocationConfigeration(configuration);  
  40.             //判断是否为第一次定位,是的话需要定位到用户当前位置  
  41.             if(isFirstIn)  
  42.             {  
  43.                 //地理坐标基本数据结构  
  44.                 LatLng latLng=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());  
  45.                 //描述地图状态将要发生的变化,通过当前经纬度来使地图显示到该位置  
  46.                 MapStatusUpdate msu= MapStatusUpdateFactory.newLatLng(latLng);  
  47.                 //改变地图状态  
  48.                 mBaiduMap.setMapStatus(msu);  
  49.                 isFirstIn=false;  
  50.                 Toast.makeText(context, bdLocation.getAddrStr(), Toast.LENGTH_SHORT).show();  
  51.             }  
  52.   
  53.   
  54.         }  
  55.     }  
4.方向传感器与定位图标方向匹配操作:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MyOrientationListener implements SensorEventListener{  
  2.   
  3.     private SensorManager mSensorManager;  
  4.     private Sensor mSensor;  
  5.     private Context mContext;  
  6.     private float lastX;  
  7.     private OnOrientationListener mOnOrientationListener;  
  8.   
  9.     public MyOrientationListener(Context context)  
  10.     {  
  11.         this.mContext=context;  
  12.     }  
  13.     public void start()  
  14.     {  
  15.         mSensorManager= (SensorManager) mContext  
  16.                 .getSystemService(Context.SENSOR_SERVICE);  
  17.         if(mSensorManager!= null)  
  18.         {  
  19.             //获得方向传感器  
  20.             mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);  
  21.         }  
  22.         //判断是否有方向传感器  
  23.         if(mSensor!=null)  
  24.         {  
  25.             //注册监听器  
  26.             mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_UI);  
  27.   
  28.         }  
  29.   
  30.   
  31.     }  
  32.     public void stop()  
  33.     {  
  34.         mSensorManager.unregisterListener(this);  
  35.   
  36.     }  
  37.     //方向改变  
  38.     @Override  
  39.     public void onSensorChanged(SensorEvent event) {  
  40.         if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)  
  41.         {  
  42.             float x=event.values[SensorManager.DATA_X];  
  43.             if(Math.abs(x-lastX)>1.0)  
  44.             {  
  45.                 if(mOnOrientationListener!=null)  
  46.                 {  
  47.                     mOnOrientationListener.onOrientationChanged(x);  
  48.                 }  
  49.             }  
  50.             lastX=x;  
  51.   
  52.         }  
  53.   
  54.     }  
  55.     public void setOnOrientationListener(OnOrientationListener listener)  
  56.     {  
  57.         mOnOrientationListener=listener;  
  58.     }  
  59.   
  60.     public interface OnOrientationListener  
  61.     {  
  62.         void onOrientationChanged(float x);  
  63.   
  64.     }  
  65.   
  66.     @Override  
  67.     public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  68.   
  69.     }  
  70. }  
如果对你有帮助,那就顶一下~~~
源码在我发布文章时还在审查过程中,因此还没有公布源码,待审核通过我会更新文章! 转载请标明出处:http://blog.csdn.net/android_for_james/article/details/51533511
  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值