开发百度地图定位APP
开发步骤:
- 切换界面:新建一个project,并切换至Project界面
- 下载百度地图SDK,使得文件架构如图所示:
- 获取本机的Android指纹码——SHA:
- 7C:DD:4E:08:D4:99:31:01:C3:0A:39:5E:9F:44:48:21:FC:57:C9:18
- 进入百度地图开发者页面、注册和登录
- 在模块的清单文件中添加百度所需权限:(该权限需要添加到Manifest文件中,application代码段外)
- 在清单文件Manifest中,在Activity组件注册的代码后,添加注册远程服务和配置应用Key的代码:
- 如出现com.baidu.location.f报错的情况,很有可能是因为下载的百度jar包不正确。
- 修改activity_main.xml文件,可根据自身需求在地图的基础上进行添加:
1. `<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:clickable=“true” />
<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/weidu"
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/jingdu"
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/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
`
- 修改MainActivuty Java文件:
在onCreate函数中对TextView和MapView组件进行定义,同时引用定位初始化:
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.bmapView);
weidu=findViewById(R.id.weidu);
jingdu=findViewById(R.id.jingdu);
address=findViewById(R.id.address);
baiduMap=mMapView.getMap();
initLocation();
在initLocation()函数中进行定位的初始化,启动定位并保存定位信息:
baiduMap.setMyLocationEnabled(true);
mLocationClient=new LocationClient(this);
MyLocationListener myListener = new MyLocationListener();
mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
// 打开gps
option.setOpenGps(true);
// 设置坐标类型
option.setCoorType("bd09ll");
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();
定义百度位置监听器MyLocationListener,将保存的定位信息传到TextView
组件中,并设置定位数据:
public void onReceiveLocation(BDLocation bdLocation) {
if (bdLocation == null || mMapView == null) {
return;
}
weidu.setText(bdLocation.getLatitude()+"");
jingdu.setText(bdLocation.getLongitude()+"");
address.setText(bdLocation.getAddrStr());
MyLocationData locData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())// 设置定位数据的精度信息,单位:米
.direction(bdLocation.getDirection()) // 此处设置开发者获取到的方向信息,顺时针0-360
.latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude())
.build();
// 设置定位数据, 只有先允许定位图层后设置数据才会生效
baiduMap.setMyLocationData(locData);
if (isFirstLocate) {
isFirstLocate = false;
LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(latLng).zoom(20.0f);
baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}
结果展示:
连接手机,将手机作为虚拟机后运行出现的界面:
遇到的问题及解决方案:
在出现以上问题并确认在控制台中所输入的信息无误时,可以核查一下求SHA1值的方法有没有出现错误,1图和2图中均可求出来SHA1值,但两个值并不相同,1图中的方法对于在Android中进行定位这一代码来说是有误的,2图的方法才正确。:
问题2:
在添加注册远程服务和配置应用Key的代码时出现如下代码的报错,可以检查一下下载的JAR包是否出现错误或者选择JAR包时未选择正确: