百度地图定位APP
一、注册和获取密钥
1.注册
搜索百度地图开发者
若有百度账号直接登录,如下图:
没有百度账号,点击右下角根据提示注册并登录
2.获取密钥
- 百度地图开发者官网
-
登录后将进入API控制台,如下图:
-
点击“创建应用”开始申请开发密钥,如下图:
-
填写应用名称,注意应用类型选择“Android SDK”、正确填写SHA1 和 程序包名(SHA1和包名的获取方法见下文)。如图:
-
获取包名
在app目录下的build.gradle文件中找到applicationId,并确保其值与AndroidManifest.xml中定义的package相同。
-
获取SHA1
进入Terminal工具,如下图所示:
找到.android文件夹下的debug.keystroe
在控制台输入命令:keytool -list -v -keystore debug.keystore
输入默认密码android,查看SHA1
填入即可
二、导入百度地图开发包
1.下载开发包
开发文档->Android 地图SDK->产品下载->自定义下载
2.将Jar包添加至项目
- 在app目录下的build.gradle文件中android块中配置sourceSets标签
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
- 选择模块视图为Project,解压文件夹至项目libs
- 右键BaiduLBS_Android.jar选择Add As Library,此时会发现在app目录下的build.gradle的dependencies块中生成了工程所依赖的jar文件的对应说明,如下所示:
三、显示地图
1. 配置AndroidManifest.xml文件
- 在application中加入如下代码配置开发密钥(AK):
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
这个密钥是在百度开发者页面里生成的,需要替换。
- 在application外部添加权限声明,需要声明的权限如下:
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 在布局文件中添加地图容器
MapView是View的一个子类,用于在Android View中放置地图。MapView的使用方法与Android提供的其他View一样。
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
3. 地图初始化
新建一个自定义的Application,在其onCreate方法中完成SDK的初始化。
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
SDKInitializer.initialize(this);
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}
在AndroidManifest.xml文件中声明该Application
android:name=".DemoApplication"
4.创建地图Activity,管理MapView生命周期
public class MainActivity extends Activity {
private MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView);
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
}
完成以上步骤即可显示:
四、定位地图
1. 配置AndroidManifest.xml文件
- 加入如下权限使用声明
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 在Application标签中声明定位的service组件
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"/>
2.布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--百度地图控件-->
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e0000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纬度:"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_Lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经度:"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_Lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地址:"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
3.开启地图的定位图层
mBaiduMap = mMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
4. 构造地图数据
我们通过继承抽象类BDAbstractListener并重写其onReceieveLocation方法来获取定位数据。
public class MyLocationListener extends BDAbstractLocationListener {
private MapView mMapView;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
private boolean isFirstLocate = true;
private LocationClientOption option;
TextView tv_Lat; //纬度
TextView tv_Lon; //经度
TextView tv_Add; //地址
//构造方法用于传递地图控件
public MyLocationListener(MapView mMapView, BaiduMap mBaiduMap, LocationClient mLocationClient,
TextView tv_Lat, TextView tv_Lon, TextView tv_Add,LocationClientOption option) {
this.mMapView = mMapView;
this.mBaiduMap = mBaiduMap;
this.mLocationClient=mLocationClient;
this.tv_Lat=tv_Lat;
this.tv_Lon=tv_Lon;
this.tv_Add=tv_Add;
this.option=option;
}
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null){
return;
}
tv_Lat.setText(location.getLatitude()+"");
tv_Lon.setText(location.getLongitude()+"");
tv_Add.setText(location.getAddrStr());
if(location.getLocType()==BDLocation.TypeGpsLocation || location.getLocType()==BDLocation.TypeNetWorkLocation){
navigateTo(location);
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
}
private void navigateTo(BDLocation location) {
if(isFirstLocate){
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(update);
isFirstLocate = false;
}
}
}
5.通过LocationClient发起定位
//定位初始化
mLocationClient = new LocationClient(this);
//通过LocationClientOption设置LocationClient相关参数
LocationClientOption option = new LocationClientOption();
//option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
//设置定位模式,三选一
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//设置需要地址信息
option.setIsNeedAddress(true);
//设置locationClientOption
mLocationClient.setLocOption(option);
//注册LocationListener监听器
MyLocationListener myLocationListener = new MyLocationListener(mMapView,mBaiduMap,
mLocationClient,tv_Lat,tv_Lon,tv_Add,option);
mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
mLocationClient.start();
6. 正确管理各部分的生命周期
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mLocationClient.stop();
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
五、结果截图
六、心得
地图显示可以在虚拟机上显示,而定位需要在真机上运行。跟着开发文档做,总体还是很顺利的,但是遇到了一个小问题,最后不断调试通过传值总算解决了。