[关闭]
@Yano 2016-04-01T08:48:43.000000Z 字数 7062 阅读 2010

Android 百度地图 SDK

Android


今天研究了百度地图 Android SDK,照着例程实现了定位功能。在实现的过程中遇到了一些麻烦,现在记录一下。

官方网址

Android 定位 SDK 的官方网址是:http://lbsyun.baidu.com/index.php?title=android-locsdk ,里面有详细的参考教程(吐槽一下,写得不够好)。

开发步骤

网页中的“开发指南”中,有详细说明,不重复造轮子。

  1. 获取密钥
  2. 配置环境
  3. 获取位置

注意事项

1.png-7kB

参考链接:http://blog.csdn.net/u010940300/article/details/43488207

代码

DEMO 下载:链接: http://pan.baidu.com/s/1pK7GIcb 密码: mh8f

DemoApplication.java

  1. package com.example.mymap;
  2. import android.app.Application;
  3. import com.baidu.mapapi.SDKInitializer;
  4. public class DemoApplication extends Application {
  5. @Override
  6. public void onCreate() {
  7. super.onCreate();
  8. // 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
  9. SDKInitializer.initialize(this);
  10. }
  11. }

MainActivity.java

  1. package com.example.mymap;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.RadioGroup;
  8. import android.widget.RadioGroup.OnCheckedChangeListener;
  9. import com.baidu.location.BDLocation;
  10. import com.baidu.location.BDLocationListener;
  11. import com.baidu.location.LocationClient;
  12. import com.baidu.location.LocationClientOption;
  13. import com.baidu.mapapi.map.BaiduMap;
  14. import com.baidu.mapapi.map.BitmapDescriptor;
  15. import com.baidu.mapapi.map.BitmapDescriptorFactory;
  16. import com.baidu.mapapi.map.MapStatus;
  17. import com.baidu.mapapi.map.MapStatusUpdateFactory;
  18. import com.baidu.mapapi.map.MapView;
  19. import com.baidu.mapapi.map.MyLocationConfiguration;
  20. import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
  21. import com.baidu.mapapi.map.MyLocationData;
  22. import com.baidu.mapapi.model.LatLng;
  23. public class MainActivity extends Activity {
  24. // 定位相关
  25. LocationClient mLocClient;
  26. public MyLocationListenner myListener = new MyLocationListenner();
  27. private LocationMode mCurrentMode;
  28. BitmapDescriptor mCurrentMarker;
  29. private static final int accuracyCircleFillColor = 0xAAFFFF88;
  30. private static final int accuracyCircleStrokeColor = 0xAA00FF00;
  31. MapView mMapView;
  32. BaiduMap mBaiduMap;
  33. // UI相关
  34. OnCheckedChangeListener radioButtonListener;
  35. Button requestLocButton;
  36. boolean isFirstLoc = true; // 是否首次定位
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41. requestLocButton = (Button) findViewById(R.id.button1);
  42. mCurrentMode = LocationMode.NORMAL;
  43. requestLocButton.setText("普通");
  44. OnClickListener btnClickListener = new OnClickListener() {
  45. public void onClick(View v) {
  46. switch (mCurrentMode) {
  47. case NORMAL:
  48. requestLocButton.setText("跟随");
  49. mCurrentMode = LocationMode.FOLLOWING;
  50. mBaiduMap
  51. .setMyLocationConfigeration(new MyLocationConfiguration(
  52. mCurrentMode, true, mCurrentMarker));
  53. break;
  54. case COMPASS:
  55. requestLocButton.setText("普通");
  56. mCurrentMode = LocationMode.NORMAL;
  57. mBaiduMap
  58. .setMyLocationConfigeration(new MyLocationConfiguration(
  59. mCurrentMode, true, mCurrentMarker));
  60. break;
  61. case FOLLOWING:
  62. requestLocButton.setText("罗盘");
  63. mCurrentMode = LocationMode.COMPASS;
  64. mBaiduMap
  65. .setMyLocationConfigeration(new MyLocationConfiguration(
  66. mCurrentMode, true, mCurrentMarker));
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. };
  73. requestLocButton.setOnClickListener(btnClickListener);
  74. RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);
  75. radioButtonListener = new OnCheckedChangeListener() {
  76. @Override
  77. public void onCheckedChanged(RadioGroup group, int checkedId) {
  78. if (checkedId == R.id.defaulticon) {
  79. // 传入null则,恢复默认图标
  80. mCurrentMarker = null;
  81. mBaiduMap
  82. .setMyLocationConfigeration(new MyLocationConfiguration(
  83. mCurrentMode, true, null));
  84. }
  85. if (checkedId == R.id.customicon) {
  86. // 修改为自定义marker
  87. mCurrentMarker = BitmapDescriptorFactory
  88. .fromResource(R.drawable.icon_geo);
  89. mBaiduMap
  90. .setMyLocationConfigeration(new MyLocationConfiguration(
  91. mCurrentMode, true, mCurrentMarker,
  92. accuracyCircleFillColor,
  93. accuracyCircleStrokeColor));
  94. }
  95. }
  96. };
  97. group.setOnCheckedChangeListener(radioButtonListener);
  98. // 地图初始化
  99. mMapView = (MapView) findViewById(R.id.bmapView);
  100. mBaiduMap = mMapView.getMap();
  101. // 开启定位图层
  102. mBaiduMap.setMyLocationEnabled(true);
  103. // 定位初始化
  104. mLocClient = new LocationClient(this);
  105. mLocClient.registerLocationListener(myListener);
  106. LocationClientOption option = new LocationClientOption();
  107. option.setOpenGps(true); // 打开gps
  108. option.setCoorType("bd09ll"); // 设置坐标类型
  109. option.setScanSpan(1000);
  110. mLocClient.setLocOption(option);
  111. mLocClient.start();
  112. }
  113. /**
  114. * 定位SDK监听函数
  115. */
  116. public class MyLocationListenner implements BDLocationListener {
  117. @Override
  118. public void onReceiveLocation(BDLocation location) {
  119. // map view 销毁后不在处理新接收的位置
  120. if (location == null || mMapView == null) {
  121. return;
  122. }
  123. MyLocationData locData = new MyLocationData.Builder()
  124. .accuracy(location.getRadius())
  125. // 此处设置开发者获取到的方向信息,顺时针0-360
  126. .direction(100).latitude(location.getLatitude())
  127. .longitude(location.getLongitude()).build();
  128. mBaiduMap.setMyLocationData(locData);
  129. if (isFirstLoc) {
  130. isFirstLoc = false;
  131. LatLng ll = new LatLng(location.getLatitude(),
  132. location.getLongitude());
  133. MapStatus.Builder builder = new MapStatus.Builder();
  134. builder.target(ll).zoom(18.0f);
  135. mBaiduMap.animateMapStatus(MapStatusUpdateFactory
  136. .newMapStatus(builder.build()));
  137. }
  138. }
  139. public void onReceivePoi(BDLocation poiLocation) {
  140. }
  141. }
  142. @Override
  143. protected void onPause() {
  144. mMapView.onPause();
  145. super.onPause();
  146. }
  147. @Override
  148. protected void onResume() {
  149. mMapView.onResume();
  150. super.onResume();
  151. }
  152. @Override
  153. protected void onDestroy() {
  154. // 退出时销毁定位
  155. mLocClient.stop();
  156. // 关闭定位图层
  157. mBaiduMap.setMyLocationEnabled(false);
  158. mMapView.onDestroy();
  159. mMapView = null;
  160. super.onDestroy();
  161. }
  162. }

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.mymap"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="14"
  8. android:targetSdkVersion="21" />
  9. <supports-screens
  10. android:anyDensity="true"
  11. android:largeScreens="true"
  12. android:normalScreens="false"
  13. android:resizeable="true"
  14. android:smallScreens="true" />
  15. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
  16. <!-- 这个权限用于进行网络定位 -->
  17. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  18. <!-- 这个权限用于访问GPS定位 -->
  19. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  20. <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
  21. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  22. <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
  23. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  24. <!-- 用于读取手机当前的状态 -->
  25. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  26. <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
  27. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  28. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  29. <!-- 访问网络,网络定位需要上网 -->
  30. <uses-permission android:name="android.permission.INTERNET" />
  31. <uses-permission android:name="android.permission.GET_TASKS" />
  32. <application
  33. android:name="com.example.mymap.DemoApplication"
  34. android:allowBackup="true"
  35. android:icon="@drawable/ic_launcher"
  36. android:label="@string/app_name"
  37. android:theme="@style/AppTheme" >
  38. <meta-data
  39. android:name="com.baidu.lbsapi.API_KEY"
  40. android:value="LSBdCjaNswoilnnX9me7w7EC5oH46QbV" />
  41. <activity
  42. android:name=".MainActivity"
  43. android:label="@string/app_name" >
  44. <intent-filter>
  45. <action android:name="android.intent.action.MAIN" />
  46. <category android:name="android.intent.category.LAUNCHER" />
  47. </intent-filter>
  48. </activity>
  49. <service
  50. android:name="com.baidu.location.f"
  51. android:enabled="true"
  52. android:process=":remote" >
  53. </service>
  54. </application>
  55. </manifest>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注