Android百度地图开发学习笔记(五)之集成语音导航和Intent调用百度地图客户端

做了之前的,接下来就是导航功能的开发了,这个其实也并不难,我直接用的官方的demo移植过来的,记一下配置和移植方法。

先看效果图:


这个弹框效果是写了一个dialog


这里用的写了一个 popupWindow实现加载效果




一 下载官方的导航demo




将里面的demo通过你对应的eclipse和AS打开就可以,主要查看BNDemoMainActivity和BNDemoGuideActivity。将其移植到自己的工程文件下。记得修改对应文件头的package。

二 打开技术文档


(1)打开语音导航技术文档,我们在完成HelloMap过程中已经通过百度地图开发平台注册了自己app的key值,这里就不需要配置了。接着打开http://yuyin.baidu.com/app为自己的应用申请语音合成服务。最后点击管理包名,配置自己的package。

一定要配置包名奥!

(2)关于demo,我们只需要按照自己需求修改BNDemoMainActivity就可以。
重点介绍几个地方。



这个是我们调用语音的最重要的地方,记住要设置成功,确保放在导航初始化成功之前就已经设置成功。
(为了确保设置成功生效,我在等待操作中设置了比较长的时间)
这个TTS_APP_ID是百度地图开发平台上创建应用的号码。

(3)发起算路
算路设置起、终点,算路偏好,是否模拟导航等参数,然后在回调函数中设置跳转至诱导。
private void routeplanToNavi(CoordinateType coType) {
    BNRoutePlanNode sNode = null;
    BNRoutePlanNode eNode = null;
	switch(coType) {
		case GCJ02: {
			sNode = new BNRoutePlanNode(116.30142, 40.05087, 
		    		"百度大厦", null, coType);
			eNode = new BNRoutePlanNode(116.39750, 39.90882, 
		    		"北京天安门", null, coType);
			break;
		}
		case WGS84: {
			sNode = new BNRoutePlanNode(116.300821,40.050969,
		    		"百度大厦", null, coType);
			eNode = new BNRoutePlanNode(116.397491,39.908749, 
		    		"北京天安门", null, coType);
			break;
		}
		case BD09_MC: {
			sNode = new BNRoutePlanNode(12947471,4846474,  
		    		"百度大厦", null, coType);
			eNode = new BNRoutePlanNode(12958160,4825947,  
		    		"北京天安门", null, coType);
			break;
		}
                case BD09LL: {
                        sNode = new BNRoutePlanNode(116.30784537597782,
                          40.057009624099436, "百度大厦", null, coType);
                        eNode = new BNRoutePlanNode(116.40386525193937, 
                         39.915160800132085, "北京天安门", null, coType);
                        break;
               }
		default : ;
	}
	if (sNode != null && eNode != null) {
        List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
        list.add(sNode);
        list.add(eNode);
        BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new DemoRoutePlanListener(sNode));
	}
}

public class DemoRoutePlanListener implements RoutePlanListener {
	
	private BNRoutePlanNode mBNRoutePlanNode = null;
	public DemoRoutePlanListener(BNRoutePlanNode node){
		mBNRoutePlanNode = node;
	}
	
	@Override
	public void onJumpToNavigator() {
		 Intent intent = new Intent(BNDemoMainActivity.this, BNDemoGuideActivity.class);
		 Bundle bundle = new Bundle();
		 bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
		 intent.putExtras(bundle);
         startActivity(intent);
	}
	@Override
	public void onRoutePlanFailed() {
		// TODO Auto-generated method stub
		
	}
}               
我选择的BD09LL百度地图坐标系
if (BaiduNaviManager.isNaviInited()) {
        			routeplanToNavi(CoordinateType.BD09LL);
        		}
这里只需要将自己的起始位置坐标和重点位置坐标的经纬度传到对应位置。将百度大厦修改成“我的位置”,北京天安门修改成“目标地址”。

sNode = new BNRoutePlanNode(116.30784537597782,
                          40.057009624099436, "百度大厦", null, coType);
                        eNode = new BNRoutePlanNode(116.40386525193937, 
                         39.915160800132085, "北京天安门", null, coType);
其他的按照技术文档仔细看,理清demo的工作原理就可以了。

三 Intent调用百度地图客户端


直接调用OK,把他放在自己的button控件点击事件代码中。
try {				
					String url="baidumap://map/direction?region=&origin="+lat_go+","+lon_go+"&destination="+addr_go+"&mode=driving";
            		Intent intent1 = Intent.getIntent(url);   
            		            if(isInstallByread("com.baidu.BaiduMap")){  
            		                    startActivity(intent1); //启动调用  
            		                    Log.e("GasStation", "百度地图客户端已经安装") ;  
            		            }else{  
            		                     Log.e("GasStation", "没有安装百度地图客户端") ;  
            		            }  
            		    } catch (URISyntaxException e) {  
            		            e.printStackTrace();  
            		    }
  private boolean isInstallByread(String packageName) {     
        return new File("/data/data/" + packageName).exists();     
       }

四 dialog弹出效果


(1)在values中创建dialog_res.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 颜色 -->
    <color name="color_normal">#ffffff</color>
    <color name="color_pressed">#d9d9d9</color>
    <color name="color_text">#131313</color>
    <!-- 字体大小 -->
    <dimen name="text_size">17sp</dimen>
    <!-- 字符串 -->
    <string name="text_initguide">内置导航</string>
    <string name="text_baiduguide">百度导航</string>
    <string name="text_cancel">取消</string>
    <!-- 样式 -->
    <style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 是否有遮盖 -->
        <item name="android:windowContentOverlay">@null</item>
        <!-- 浮于Activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- Dialog以外的区域模糊效果 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- Dialog进入及退出动画 -->
        <item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item>
    </style>
    <!-- dialog从下面进出动画 -->
    <style name="BottomDialogAnimation" parent="@android:style/Animation.Dialog">
        <item name="android:windowEnterAnimation">@anim/anim_dialog_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/anim_dialog_bottom_out</item>
    </style>
   
</resources>
(2)在layout创建dialog_layout..xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical">
    <Button
        android:id="@+id/initguide"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:text="@string/text_initguide"
        android:textColor="@color/color_text"
        android:textSize="@dimen/text_size"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#9e9e9e"/>

    <Button
        android:id="@+id/baiduguide"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:text="@string/text_baiduguide"
        android:textColor="@color/color_text"
        android:textSize="@dimen/text_size"/>

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="8dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:text="@string/text_cancel"
        android:textColor="@color/color_text"
        android:textSize="@dimen/text_size"/>
</LinearLayout>
(3)在res文件下创建一个anim文件夹,在里面创建anim_dialog_bottom_in.xml和anim_dialog_bottom_out.xml,实现动画弹入和弹出效果。
anim_dialog_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="200"
     android:fromYDelta="100%"
     android:toYDelta="0"/>
anim_dialog_bottom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="200"
     android:fromYDelta="0"
     android:toYDelta="100%" />
到这里布局算写好了。
效果如下



(4)调用dialog
   public void dialog_show()
    {
	    dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
        //初始化控件
        inflate.findViewById(R.id.initguide).setOnClickListener(this);
        inflate.findViewById(R.id.baiduguide).setOnClickListener(this);
        inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        if(dialogWindow == null){
            return;
        }
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框    
    }

五 效果popupWindow加载等待效果


(1)使用ProgressBar,在popupWindow布局中添加。
我写的布局样式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" 
    android:background="#F0F3F6" 
    >    
  
    <RelativeLayout  
        android:layout_width="160dp"  
        android:layout_height="160dp"  
        android:layout_centerInParent="true"  
        >  
  
        <LinearLayout  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:gravity="center"  
            android:orientation="vertical" 
            android:background="@+color/black">  
              
              <ProgressBar
        		android:id="@+id/progressBar4"
        		style="?android:attr/progressBarStyle"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content"
        		android:layout_centerInParent="true"
        		/>       
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="加载中..."  
                android:layout_marginTop="10dp"  
                android:textColor="#fff"  
                android:textSize="20sp"  
        />  
        </LinearLayout>  
  
    </RelativeLayout>  
  
</RelativeLayout> 
效果如图

调用popupWindow
	                 final PopupWindow popupWindow = new PopupWindow();
		         popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		         popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		         popupWindow.setFocusable(true);
		         view = LayoutInflater.from(this).inflate(R.layout.popup_window,null);
		         popupWindow.setContentView(view);
		         popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,0,0);

		         new Handler().postDelayed(new Runnable() {
		             @Override
		             public void run() {
		               	Intent intent=new Intent(MapActivity.this,NaviInitActivity.class);
		            	Bundle bundle = new Bundle();
		            	bundle.putString("start_wd", lat_go);
						bundle.putString("start_jd", lon_go);
						DecimalFormat format = new DecimalFormat("#.00");		    
						bundle.putString("end_wd", format.format(maker_lat));
						bundle.putString("end_jd",format.format(maker_lon));
						intent.putExtras(bundle);
						startActivity(intent); 
		                popupWindow.dismiss();
		             }
		         },3000);
好啦,百度地图开发学习全部介绍完毕啦,源码会等我毕业设计答辩后整理上传。百度地图虽然还有很多高级开发吧,但学习到这里应该收获很大啦,学习看技术文档很关键!嘻嘻。

有问题私信或者我的邮箱598940712@qq.com
注:本人学习电子信息工程的,也想请大神指导我一下。
委屈



























  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值