这两天道长在弄百度地图,开发中也遇到了一些问题,在这里和大家分享一下(额,主要是记录一下,省的下次又忘了)
一、准备
百度地图密钥申请
百度地图SDK下载
如果需要以前版本的SDK,请点这里的传送门
还有其他没有搞定的话可以自己去百度地图开放平台自行研究一下。
二、定位
1.百度地图配置
- 添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
- 添加百度密钥
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="百度密钥" />
- 添加百度定位服务
<!-- 百度定位SDK service BEGIN -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
2.百度地图定位实现
- 初始化百度地图
private void initMap() {
// 获取地图控件引用
mMapView.removeViewAt(2);
// 不显示缩放比例尺
mMapView.showZoomControls(false);
// 百度地图
mBaiduMap = mMapView.getMap();
}
- 初始化定位
private void initLocation() {
// 定位客户端的设置
mLocationClient = new LocationClient(getApplicationContext());
mLocationListener = new MyLocationListener();
// 注册监听
mLocationClient.registerLocationListener(mLocationListener);
// 配置定位
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(3000); // 调用监听时间间隔
mLocationClient.setLocOption(option);
}
- 实现定位监听接口
/**
* 定位SDK监听接口
*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirst == true){
LatLng cenpt = new LatLng(location.getLatitude(), location.getLongitude());
// LatLng cenpt = new LatLng(40.051558, 116.314941);
MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(14).build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.animateMapStatus(mMapStatusUpdate);
isFirst = false;
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
- 定位到某一个点
这个是根据经纬度定位并移动到一个点,然后在上面添加图层
private void refreshActivityView() {
// 改变地图状态 40.051558,116.314941 29.806651,121.606983
// LatLng cenpt = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
LatLng cenpt = new LatLng(40.051558, 116.314941);
MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(14).build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.animateMapStatus(mMapStatusUpdate);
OverlayOptions ooA = new MarkerOptions().position(cenpt).icon(BitmapDescriptorFactory
.fromResource(R.drawable.club_track_location))
.zIndex(10).draggable(true);
mBaiduMap.addOverlay(ooA);
}
三、轨迹
轨迹就是在百度地图上添加一个图层,把定位信息绘制在图层上
- 绘制轨迹图层
private void drawTrackLine(BDLocation location) {
dot = new LatLng(location.getLatitude(), location.getLongitude());
if (startTrack == true) {
line.add(dot);
if (line.size() == 1) {
// 这里绘制起点
drawStart(line);
if (isPause != true) {
ToastUtil.showCenter(ClubTrackActivity.this, "轨迹已开启");
}
} else if (line.size() > 3) {
points_tem = line.subList(line.size() - 2, line.size());
options = new PolylineOptions().color(0xfe12b7f5).width(10)
.points(points_tem);
mBaiduMap.addOverlay(options);
}
}
}
- 绘制起点、终点
/**
* 绘制起点,取前n个点坐标的平均值绘制起点
*
* @param points2
*/
public void drawStart(List<LatLng> points2) {
double myLat = 0.0;
double myLng = 0.0;
for (LatLng ll : points2) {
myLat += ll.latitude;
myLng += ll.longitude;
}
LatLng avePoint = new LatLng(myLat / points2.size(), myLng
/ points2.size());
line.add(avePoint);
OverlayOptions ooA;
if (setDot == false) {
ooA = new MarkerOptions().position(avePoint).icon(BitmapDescriptorFactory
.fromResource(R.drawable.club_track_start))
.zIndex(4).draggable(true);
} else {
ooA = new DotOptions().center(avePoint).color(0xfe12b7f5).radius(15);
}
mBaiduMap.addOverlay(ooA);
}
/**
* 绘制终点。
*
* @param points2
*/
protected void drawEnd(List<LatLng> points2) {
double myLat = 0.0;
double myLng = 0.0;
if (points2.size() >= 2) {
for (int i = points2.size() - 2; i < points2.size(); i++) {
LatLng ll = points2.get(i);
myLat += ll.latitude;
myLng += ll.longitude;
}
LatLng avePoint = new LatLng(myLat / 2, myLng / 2);
OverlayOptions ooA;
if (setDot == false) {
ooA = new MarkerOptions().position(dot).icon(BitmapDescriptorFactory
.fromResource(R.drawable.club_track_close))
.zIndex(4).draggable(true);
} else {
ooA = new DotOptions().center(avePoint).color(0xfe12b7f5).radius(15);
mBaiduMap.addOverlay(ooA);
}
setDotSuc = true;
mBaiduMap.addOverlay(ooA);
} else {
if (isPause == false && setDot == false) {
OverlayOptions ooA = new MarkerOptions().position(dot).icon(BitmapDescriptorFactory
.fromResource(R.drawable.club_track_close))
.zIndex(4).draggable(true);
setDotSuc = true;
mBaiduMap.addOverlay(ooA);
}
}
}
四、遇到的bug
1.白板
描述:加载地图时显示白板,没有网格
原因:经纬度传反了
2.加载崩溃
描述:百度地图在华为mate8上加载崩溃,异常内容:
java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.wanbu.dascom-1/lib/arm64/libBaiduMapSDK_v3_2_0_15.so" is 32-bit instead of 64-bit
原因:Android5.0以后支持64位的手机芯片,但是百度地图在3.5.0开始对64位的手机芯片开始支持,在Android5.0+的64位手机上使用3.5.0之前版本的百度地图SDK会崩溃。
解决方法:更新百度地图SDK
3.定位到非洲
描述:百度地图定位到非洲几内亚湾。当时道长看到这个现象是崩溃的,这是什么鬼~~~
使用百度地图的Api查找原因:
int code = location.getLocType();
ToastUtil.showCenter(getApplicationContext(), "key= " + code);
道长返回的code值为505,可以查看源码:
public void setLocType(int var1) {
this.mLocType = var1;
switch(var1) {
case 61:
this.setLocTypeDescription("GPS location successful!");
this.setUserIndoorState(0);
break;
case 62:
this.setLocTypeDescription("Location failed beacuse we can not get any loc information!");
break;
case 63:
case 67:
this.setLocTypeDescription("Offline location failed , please check the net (wifi/cell)!");
break;
case 66:
this.setLocTypeDescription("Offline location successful!");
break;
case 161:
this.setLocTypeDescription("NetWork location successful!");
break;
case 162:
this.setLocTypeDescription("NetWork location failed because baidu location service can not decrypt the request query, please check the so file !");
break;
case 167:
this.setLocTypeDescription("NetWork location failed because baidu location service can not caculate the location!");
break;
case 505:
this.setLocTypeDescription("NetWork location failed because baidu location service check the key is unlegal, please check the key in AndroidManifest.xml !");
break;
default:
this.setLocTypeDescription("UnKnown!");
}
}
可以看到道长的key是违法的,重新申请了一个key更换上就可以了
关于百度地图的暂时到这里,以后有新的内容再分享。希望这篇博客能为你提供一些帮助
demo中的百度key由于某些原因去掉了,记得使用时要重新填写!重新填写!重新填写!