重要:为进一步采取加强对最终用户个人信息的安全保护措施,从地图SDK v7.5.0版本起,请开发者务必确保调用SDK任何接口前先调用隐私合规接口setAgreePrivacy,否则可能会无法正常使用相关功能。具体可参考开发指南-开发注意事项-隐私合规接口说明
public class MainActivity extends Activity {private MapView mMapView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取地图控件引用mMapView = (MapView) findViewById(R.id.bmapView);mBaiduMap = mMapView.getMap();//显示卫星图层mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);}@Overrideprotected void onResume() {super.onResume();//在activity执行onResume时必须调用mMapView. onResume ()mMapView.onResume();}@Overrideprotected void onPause() {super.onPause();//在activity执行onPause时必须调用mMapView. onPause ()mMapView.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();//在activity执行onDestroy时必须调用mMapView.onDestroy()mMapView.onDestroy();}}
@Overridepublic void onBaseIndoorMapMode(boolean in, MapBaseIndoorMapInfo mapBaseIndoorMapInfo) {if (in) {// 进入室内图// 通过获取回调参数 mapBaseIndoorMapInfo 便可获取室内图信息,包含楼层信息,室内ID等} else {// 移除室内图}}});//实现楼层间地图切换,展示不同楼层的室内图SwitchFloorError switchFloorError = mBaiduMap.switchBaseIndoorMapFloor(strFloor, floorID);//上面代码中,strFloor表示室内图楼层,格式为F1,B1… strID 表示室内图ID;返回值switchFloorError 用于标识楼层切换错误信息, 具体如下://切换楼层成功SWITCH_OK// 切换楼层, 室内ID信息错误FLOOR_INFO_ERROR,//楼层溢出FLOOR_OVERLFLOW,//切换楼层室内ID与当前聚焦室内ID不匹配FOCUSED_ID_ERROR,//切换楼层失败SWITCH_ERROR
/*** 初始化地图SDK*/public class DemoApplication extends Application {@Overridepublic void onCreate() {super.onCreate();//在使用SDK各组件之前初始化context信息,传入ApplicationContextSDKInitializer.initialize(this);//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。SDKInitializer.setCoordType(CoordType.BD09LL);}}/***创建Activity, 地图生命周期管理*/public class MainActivity extends Activity {private MapView mMapView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取地图控件引用mMapView = (MapView) findViewById(R.id.bmapView);//地图状态参数配置(经纬度是国外)MapStatus ms = new MapStatus.Builder().zoom(10).target(new LatLng(51.50556,-0.07556 )).build();MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(ms);//更新地图状态mBaiduMap.animateMapStatus(u);}@Overrideprotected void onResume() {super.onResume();//在activity执行onResume时必须调用mMapView. onResume ()mMapView.onResume();}@Overrideprotected void onPause() {super.onPause();//在activity执行onPause时必须调用mMapView. onPause ()mMapView.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();//在activity执行onDestroy时必须调用mMapView.onDestroy()mMapView.onDestroy();}}
//创建markerMarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA);//添加markermMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));private Animation getScaleAnimation() {//创建缩放动画ScaleAnimation mScale = new ScaleAnimation(1f, 2f, 1f);//设置动画执行时间mScale.setDuration(2000);//动画重复模式mScale.setRepeatMode(Animation.RepeatMode.RESTART);//动画重复次数mScale.setRepeatCount(1);//设置缩放动画监听mScale.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart() {}@Overridepublic void onAnimationEnd() {}@Overridepublic void onAnimationCancel() {}@Overridepublic void onAnimationRepeat() {}});return mScale;}/*** 开启单边缩放动画 X或Y方向*/public void startSingleScaleAnimation() {//marker设置动画mMarkerG.setAnimation(getSingleScaleAnimation());//开启marker动画mMarkerG.startAnimation();}
/*** 周边poi检索示例*/public void nearbyPoiSearch() {//创建poi检索实例PoiSearch poiSearch = PoiSearch.newInstance();//创建poi监听者OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {@Overridepublic void onGetPoiResult(PoiResult result) {//获取POI检索结果}......};//设置poi监听者该方法要先于检索方法searchNearby(PoiNearbySearchOption)前调用,否则会在某些场景出现拿不到回调结果的情况poiSearch.setOnGetPoiSearchResultListener(poiListener);//设置请求参数PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword("餐厅")//检索关键字.location(center)//检索位置.pageNum(loadIndex)//分页编号,默认是0页.pageCapacity(20)//设置每页容量,默认10条.radius(radius);//附近检索半径//发起请求poiSearch.searchNearby(nearbySearchOption);//释放检索对象poiSearch.destroy();}
/*** 驾车路线规划代码示例*/public void drivingRoute() {//创建驾车线路规划检索实例;RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance();//创建驾车线路规划检索监听者;OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {public void onGetDrivingRouteResult(DrivingRouteResult result) {//获取驾车线路规划结果}......};//设置驾车线路规划检索监听者,该方法要先于检索方法drivingSearch(DrivingRoutePlanOption)前调用,否则会在某些场景出现拿不到回调结果的情况routePlanSearch.setOnGetRoutePlanResultListener(listener);//准备检索起、终点信息;PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "龙泽");PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "西单");//发起驾车线路规划检索;routePlanSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(enNode));
//设置导航的起终点信息LatLng startPt = new LatLng(40.047416,116.312143);LatLng endPt = new LatLng(40.048424, 116.313513);WalkNaviLaunchParam param = new WalkNaviLaunchParam().stPt(startPt).endPt(endPt);// 使用步行导航前,需要初始化引擎初。WalkNavigateHelper.getInstance().initNaviEngine(this, new IWEngineInitListener() {@Overridepublic void engineInitSuccess() {Log.d(LTAG, "引擎初始化成功");routePlanWithParam();}@Overridepublic void engineInitFail() {Log.d(LTAG, "引擎初始化失败");}});/*** 引擎初始化成功之后,发起导航算路。算路成功后,在回调函数中设置跳转至诱导页面。* 开始算路*/public void routePlanWithParam() {WalkNavigateHelper.getInstance().routePlanWithParams(param, new IWRoutePlanListener() {@Overridepublic void onRoutePlanStart() {Log.d(LTAG, "开始算路");}@Overridepublic void onRoutePlanSuccess() {Log.d(LTAG, "算路成功,跳转至诱导页面");Intent intent = new Intent();intent.setClass(BNaviMainActivity.this, WNaviGuideActivity.class);startActivity(intent);}@Overridepublic void onRoutePlanFail(WalkRoutePlanError error) {Log.d(LTAG, "算路失败");}});}//初始化步行导航过程管理类WalkNavigateHelper mNaviHelper = WalkNavigateHelper.getInstance();// 在导航页面WNaviGuideActivity.this的onCreate方法中,调用mNaviHelper.onCreate(WNaviGuideActivity.this)创建诱导View。View view = mNaviHelper.onCreate(WNaviGuideActivity.this);if (view != null) {setContentView(view);}// 开始导航mNaviHelper.startWalkNavi(WNaviGuideActivity.this);//在步行导航页面对应的acticity的生命周期方法中分别调用WalkNavigateHelper类中的对应生命周期方法。@Overrideprotected void onResume() {super.onResume();mNaviHelper.resume();}@Overrideprotected void onPause() {super.onPause();mNaviHelper.pause();}@Overrideprotected void onDestroy() {super.onDestroy();mNaviHelper.quit();}//导航状态信息,主要包括导航开始、结束,导航过程中偏航、偏航结束、诱导信息(包含诱导默认图标、诱导类型、诱导信息、剩余距离、时间、振动回调等mNaviHelper.setRouteGuidanceListener(this, new IWRouteGuidanceListener() {@Overridepublic void onRouteGuideIconUpdate(Drawable icon) {//诱导图标更新}@Overridepublic void onRouteGuideKind(RouteGuideKind routeGuideKind) {//诱导枚举信息}@Overridepublic void onRoadGuideTextUpdate(CharSequence charSequence, CharSequence charSequence1) {//诱导信息}@Overridepublic void onRemainDistanceUpdate(CharSequence charSequence) {// 总的剩余距离}@Overridepublic void onRemainTimeUpdate(CharSequence charSequence) {//总的剩余时间}@Overridepublic void onGpsStatusChange(CharSequence charSequence, Drawable drawable) {//GPS状态发生变化,来自诱导引擎的消息}@Overridepublic void onRouteFarAway(CharSequence charSequence, Drawable drawable) {//偏航信息}@Overridepublic void onRoutePlanYawing(CharSequence charSequence, Drawable drawable) {//偏航规划中的信息}@Overridepublic void onReRouteComplete() {//重新算路成功}@Overridepublic void onArriveDest() {//到达目的地}@Overridepublic void onVibrate() {//震动}});}//设置诱导信息回调监听,此组件只提供导航过程中的文本输出,不包含语音播报功能,需要自行传入对应的语音回调,形成播报功能。建议使用百度语音识别服务SDK。//获取语音播报文本方法(注:该接口需要在startWalkNavi方法之前调用,否则不会有回调):mNaviHelper.setTTsPlayer(new IBTTSPlayer() {/*** 诱导文本回调* @param s 诱导文本* @param b 是否抢先播报* @return*/@Overridepublic int playTTSText(String s, boolean b) {return 0;}});
//通过设置BikeNaviLaunchParam对象中的vehicle的值区分:vehicle :0:普通骑行导航 ; 1:电动车骑行导航,不设置vehicle的值时,默认为0 普通骑行导航。startPt = new LatLng(40.047416,116.312143);endPt = new LatLng(40.048424, 116.313513);param = new BikeNaviLaunchParam().stPt(startPt).endPt(endPt).vehicle(0);// 使用骑行导航前,需要初始化骑行导航引擎。BikeNavigateHelper.getInstance().initNaviEngine(this, new IBEngineInitListener() {@Overridepublic void engineInitSuccess() {Log.d(LTAG, "引擎初始化成功");routePlanWithParam();}@Overridepublic void engineInitFail() {Log.d(LTAG, "引擎初始化失败");}});/*** 算路设置起、终点参数,然后在回调函数中设置跳转至诱导页面* 开始算路*/public void routePlanWithParam() {param = new BikeNaviLaunchParam().stPt(startPt).endPt(endPt);BikeNavigateHelper.getInstance().routePlanWithParams(param, new IBRoutePlanListener() {@Overridepublic void onRoutePlanStart() {Log.d(LTAG, "开始算路");}@Overridepublic void onRoutePlanSuccess() {Log.d(LTAG, "算路成功,跳转至诱导页面");Intent intent = new Intent();intent.setClass(BNaviMainActivity.this, BNaviGuideActivity.class);startActivity(intent);}@Overridepublic void onRoutePlanFail(BikeRoutePlanError error) {Log.d(LTAG, "算路失败");}});}// 获取诱导页面地图展示View//创建诱导View,并接收回调事件。在activity生命周期内调用诱导BikeNavigateHelper对应的生命周期函数。View view = mNaviHelper.onCreate(BNaviGuideActivity.this);if (view != null) {setContentView(view);}// 开始导航mNaviHelper.startBikeNavi(BNaviGuideActivity.this);// 设置诱导监听, 主要包括导航开始、结束,导航过程中偏航、偏航结束、诱导信息(包含诱导默认图标、诱导类型、诱导信息、剩余距离、时间、振动回调等。mNaviHelper.setRouteGuidanceListener(this, new IBRouteGuidanceListener() {@Overridepublic void onRouteGuideIconUpdate(Drawable icon) {}@Overridepublic void onRouteGuideKind(RouteGuideKind routeGuideKind) {}@Overridepublic void onRoadGuideTextUpdate(CharSequence charSequence, CharSequence charSequence1) {}@Overridepublic void onRemainDistanceUpdate(CharSequence charSequence) {}@Overridepublic void onRemainTimeUpdate(CharSequence charSequence) {}@Overridepublic void onGpsStatusChange(CharSequence charSequence, Drawable drawable) {}@Overridepublic void onRouteFarAway(CharSequence charSequence, Drawable drawable) {}@Overridepublic void onRoutePlanYawing(CharSequence charSequence, Drawable drawable) {}@Overridepublic void onReRouteComplete() {}@Overridepublic void onArriveDest() {}@Overridepublic void onVibrate() {}@Overridepublic void onGetRouteDetailInfo(BikeRouteDetailInfo bikeRouteDetailInfo) {}});//设置诱导信息回调监听,此组件只提供导航过程中的文本输出,不包含语音播报功能,需要自行传入对应的语音回调,形成播报功能。建议使用百度语音识别服务SDK。//获取语音播报文本方法(注:该接口需要在startWalkNavi方法之前调用,否则不会有回调):mNaviHelper.setTTsPlayer(new IBTTSPlayer() {/*** 诱导文本回调* @param s 诱导文本* @param b 是否抢先播报* @return*/@Overridepublic int playTTSText(String s, boolean b) {return 0;}});
下一篇
本篇文章对您是否有帮助?