一、地图
1、集成基础地图功能,进入页面地图显示灰白网格,不显示地图
解:一般因为你的清单文件中配置的百度KEY不正确,亦或各个版本的arm包没有加入在工程lib下面或是加的不全,用户权限也必不可少
正确的看下图
2、地图上的Logo,缩放,定位,比例尺等图标及按钮的隐藏和显示
/**
*
* @Author Scw
* @Email [email protected]
* @param mMapView
* @param goneLogo//设置logo是否显示
* @param goneZoomControls//设置缩放按钮是否显示
* @param goneScaleControl//设置比例尺是否显示
* @param goneCompassEnabled//设置指南针是否显示
* @return void
* @throws
* @Title: goneMapViewChild
* @Description: 隱藏百度logo亦或百度自帶的縮放鍵
*/
public static void goneMapViewChild(MapView mMapView, boolean goneLogo,
boolean goneZoomControls, boolean goneScaleControl, boolean goneCompassEnabled) {
int count = mMapView.getChildCount();
for (int i = 0; i < count; i++) {
View child = mMapView.getChildAt(i);
if (child instanceof ImageView && goneLogo) { // 隐藏百度logo
child.setVisibility(View.GONE);
}
// 已过时
// if (child instanceof ZoomControls && goneZoomControls) { // 隐藏百度的縮放按鍵
// child.setVisibility(View.GONE);
// }
mMapView.showZoomControls(goneZoomControls);
mMapView.showScaleControl(goneScaleControl);
mMapView.getMap().getUiSettings().setCompassEnabled(goneCompassEnabled);
}
}
3、自定义定位按钮实现
隐藏API里面的定位按钮,添加自己的按钮布局,吊起百度定位,获取返回结果操作地图(方法如下)
一、
/**
* 地图展示定位
*
* @param location
*/
private void showLocationOnMap(BDLocation location) {
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
// BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
// .fromResource(R.drawable.icon_location);
MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, null);
mBaiduMap.setMyLocationConfigeration(config);
// 当不需要定位图层时关闭定位图层
// mBaiduMap.setMyLocationEnabled(false);
BaiduMapUtil.showMarkerByResource(location.getLatitude(), location.getLongitude(), lin_park_layout.getVisibility() == View.VISIBLE ? R.drawable.defalut_park_big : R.drawable.default_park, mBaiduMap, 100, false);
}
二、
下面方法取其一便可
/**
* @param lat
* @param lon
* @param mBaiduMap
* @return void
* @throws
* @Title: moveToTarget
* @Description:移動到該點
*/
public static void moveToTarget(double lat, double lon, BaiduMap mBaiduMap) {
try {
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(
lat, lon)));
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param mLatLng
* @param mBaiduMap
* @return void
* @throws
* @Title: moveToTarget
* @Description:移動到該點
*/
public static void moveToTarget(LatLng mLatLng, BaiduMap mBaiduMap) {
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(mLatLng));
}
4、自定义缩放按钮的实现
隐藏API按钮,绘制自己的布局,点击监听中添加如下方法
/**
* @param zoomLevel
* @param mBaiduMap
* @return void
* @throws
* @Title: setZoom
* @Description: 縮放地圖的
*/
public static void setZoom(float zoomLevel, BaiduMap mBaiduMap) {
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomTo(zoomLevel));
}
/**
* @param mMapView
* @return void
* @throws
* @Title: zoomIn
* @Description: 放大地圖
*/
public static void zoomInMapView(MapView mMapView) {
try {
BaiduMapUtil.setZoom(
mMapView.getMap().getMapStatus().zoom + 1,
mMapView.getMap());
} catch (NumberFormatException e) {
}
}
/**
* @param mMapView
* @return void
* @throws
* @Title: zoomOut
* @Description: 縮小地圖
*/
public static void zoomOutMapView(MapView mMapView) {
try {
BaiduMapUtil.setZoom(
mMapView.getMap().getMapStatus().zoom - 1,
mMapView.getMap());
} catch (NumberFormatException e) {
}
}
5、自定义比例尺
隐藏原有的,绘制自定义布局,实现
BaiduMap.OnMapStatusChangeListener
接口代码
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
}
@Override
public void onMapStatusChange(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
txt_maplevel.setText(bmapView.getMapLevel() > 1000 ?
(bmapView.getMapLevel() / 1000) + "公里" : bmapView.getMapLevel() + "米");
}
二、定位
定位失败,用户权限是否添加齐全,百度定位service是否在清单文件配置(下图黄色部分),KEY是否配置正确
三、导航(也是我写本文的原因)
一、最近在做一个项目基于地图功能,有定位,基础地图,导航,开发工具Android studio 2.2 Preview 6,()之前开发过Eclipse版本没有什么大问题)最初集成的时候,总是会提示这样的错误
这个原因说的很清楚是因为API没有找到对应arm下面的.so包,但是上图可以清晰的看见我的的arm配置是正常的并不缺少,如果是这样你就可以想到 是不是他找到的并不是我们配置的arm包,然后去工程目录下面搜索一下arm相关的你会发现有很多这样的包,上图中有v7 v8很多种包,但是这些包我们配置的时候并没有配置他们 只有一个基础的arm 的包,为什么会有呢?打开其中一个,你会发现里面的.so根本就不是我们配置的so文件而是你工程里面引用的其他第三方包封装起来的,去build.gradle中查找一下,就知道了 ,就是这样类似的包,它本身封装了arm再被我们引入工程会与我们工程本身的arm起冲突,导致API找不到正确的arm包,去掉之后即可。
二、高版本Android手机问题
看图,大概的意思就是文件路径反射异常,导致找不到so文件,从而导致初始化失败。亲测Android 6.0以下手机正常无异,Android 6.0 报上图错误,查看build.gradle最大SDK支持23,这就是根本问题,或许是因为百度导航SDK还不够完善吧,所以直接不设置整个项目的targetSDKVersion或者设置targetSDKVersion小于23,运行Android 6.0手机,初始化成功,导航正常
三、关于ClassNotFound问题
一般分为2种情况:
1、启动activity的时候,在AndroidManifest.xml 可能压根你就忘了添加一个activity,或者说activity的包名或者名字写错了,这个犯错几率很高的
2、你的使用的class,是一个外部的JAR包,当在工程中编译使用时,发布成APK并没有包含JAR文件,所以APK在执行的时候就找不到JAR文件,会报错。
所以一定在“Order and Export”里,勾选使用的jar包。切记!
Android studio File -> Settings ->Build, Execution, Deployment -> Instant Run -> Enable 去掉,然后再运行试试。
Eclipse
四、
退出导航是出现:
Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.vi.VDeviceAPI$1@27d8b76e that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.vi.VDeviceAPI$1@27d8b76e that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:900)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:701)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1639)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1619)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1613)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:488)
at com.baidu.navisdk.vi.VDeviceAPI.setNetworkChangedCallback(VDeviceAPI.java:287)
at com.baidu.navisdk.jni.nativeif.JNITrajectoryControl.startRecord(Native Method)
at com.baidu.navisdk.jni.nativeif.JNITrajectoryControl.startRecord(JNITrajectoryControl.java:54)
at com.baidu.navisdk.adapter.base.TrajectoryManager.startRecord(TrajectoryManager.java:81)
at com.baidu.navisdk.adapter.BNRouteGuideManager.onCreate(BNRouteGuideManager.java:166)
at com.adayome.bddemo.bdNaviGuideActivity.onCreate(bdNaviGuideActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5941)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
在onDestory中加入
VDeviceAPI.unsetNetworkChangedCallback();
@Override
protected void onDestroy() {
super.onDestroy();
VDeviceAPI.unsetNetworkChangedCallback();
}
五、
出现如下警告
09-14 10:21:09.061: D/StatSDK(25717): null,can't find information for key:BaiduMobAd_CELL_LOCATION
09-14 10:21:09.061: D/getLocation cell:(25717): [10369,13181,-1]
09-14 10:21:09.061: D/StatSDK(25717): null,can't find information for key:BaiduMobAd_GPS_LOCATION
09-14 10:21:09.071: D/statsdk(25717): location: Location[gps 23.120855,114.501520 acc=43 et=+1d21h40m45s447ms alt=155.6999969482422 vel=0.0 {Bundle[mParcelledData.dataSize=40]}]
09-14 10:21:09.071: D/StatSDK(25717): null,can't find information for key:BaiduMobAd_WIFI_LOCATION
在AndroidManifest.xml中添加如下代码片段即可
六、
如下警告
Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.PhoneStatusReceiver@11d4d01f that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.PhoneStatusReceiver@11d4d01f that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:900)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:701)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at java.lang.reflect.Method.invoke(Method.java:372)
Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.SDCardListener@18e137be that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.SDCardListener@18e137be that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:900)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:701)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1639)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1619)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1613)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:488)
at com.baidu.navisdk.BNaviModuleManager.initSDCardListener(BNaviModuleManager.java:200)
at com.baidu.navisdk.BNaviModuleManager.initListenersForMap(BNaviModuleManager.java:92)
at com.baidu.navisdk.adapter.BaiduNaviManager.init(BaiduNaviManager.java:546)
at com.adayome.bddemo.Fragment1.initNavi(Fragment1.java:345)
at com.adayome.bddemo.Fragment1.onCreate(Fragment1.java:287)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:13420)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2708)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1081)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5818)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
(16644): baidu location service stop ...
Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.NetworkListener@37cc0e79 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.adayome.bddemo.MainActivity has leaked IntentReceiver com.baidu.navisdk.util.listener.NetworkListener@37cc0e79 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:900)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:701)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1639)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1619)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:495)
at com.baidu.navisdk.BNaviModuleManager.initNetworkListener(BNaviModuleManager.java:187)
at com.baidu.navisdk.BNaviModuleManager.initListenersForMap(BNaviModuleManager.java:91)
at com.baidu.navisdk.adapter.BaiduNaviManager.init(BaiduNaviManager.java:546)
at com.adayome.bddemo.Fragment1.initNavi(Fragment1.java:345)
at com.adayome.bddemo.Fragment1.onCreate(Fragment1.java:287)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:13420)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2708)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2715)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1081)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5818)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
在onDestory中添加
if (BaiduNaviSDKProxy.isNaviInited())
BaiduNaviSDKProxy.uninit();
七、导航退出时出现一瞬的黑屏
在AndroidManifest.xml中添加透明主题
八、导航成功,但是进入诱导页地图全黑或TTS只播报“百度导航,一路畅通”,同时或许会伴有如上4-6之间的错误
这个是频繁初始化导致的,其实真的使用导航只需要在使用之前做全局的初始化,例如我直接在Fragment的主Activity里面初始化,Fragment中用的时候直接调用里面的导航方法,不再进行初始化操作,第一、耗时,第二、易出错,因为导航初始化的时候是会对系统进行IO读写等操作,这些操作既耗时,且易报错。
不在APP退出之前对导航进行销毁操作,如果销毁不干净会直接影响下次初始化,以及后续导航的使用
九、提醒
如果同时集成地图定位导航等功能需要在http://lbsyun.baidu.com/sdk/download?selected=navifunctionwithtts这里选择好功能下载组合包使用,下图展示包含了地图定位和导航,同事如果需要语音播报公共 不仅要配置好百度KEY 还需添加到TTS白名单中http://app.navi.baidu.com/ttsregister/appinfo
(希望能帮到大家,如有异议或者补充欢迎留言)