1. 程式人生 > >高德地圖---路徑規劃---語音導航的開發

高德地圖---路徑規劃---語音導航的開發

第一步匯入高德地圖導航開發包及對應的功能類

這裡寫圖片描述

第二步路徑規劃的類繼承導航的介面

Activity_NaviChoose extends Activity implements OnRouteSearchListener,AMapNaviListener

OnRouteSearchListener路徑規劃的介面
AMapNaviListener導航的介面

第三步路徑規劃

自駕路徑規劃

/**
     * 自駕路徑規劃
     */
    public void dringRoute() {
        Log.e("route", "--------------dringRoute---------------"
); RouteSearch routeSerch=new RouteSearch(Activity_NaviChoose.this); routeSerch.setRouteSearchListener(this); LatLonPoint start_lat=new LatLonPoint(la_start, ln_start); //起點 LatLonPoint end_lat=new LatLonPoint(la_end,ln_end);//終點 RouteSearch.FromAndTo fromAndTo = new
RouteSearch.FromAndTo( start_lat, end_lat); DriveRouteQuery query = new DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");// 第一個引數表示路徑規劃的起點和終點,第二個引數表示駕車模式,第三個引數表示途經點,第四個引數表示避讓區域,第五個引數表示避讓道路 routeSerch.calculateDriveRouteAsyn(query);// 非同步路徑規劃駕車模式查詢 // 非同步路徑規劃公交模式查詢
}

步行路徑規劃

/**
     * 步行路徑規劃
     */
    public void warkRoute() {
        Log.e("route", "--------------warkRoute---------------");
        RouteSearch routeSerch=new RouteSearch(Activity_NaviChoose.this);

        routeSerch.setRouteSearchListener(this);
        LatLonPoint start_lat=new LatLonPoint(la_start, ln_start); //定位本地座標
        LatLonPoint end_lat=new LatLonPoint(la_end,ln_end);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                start_lat, end_lat);
        WalkRouteQuery query=new WalkRouteQuery(fromAndTo, RouteSearch.WalkDefault);
        routeSerch.calculateWalkRouteAsyn(query);
    }

公交路徑規劃

public void busRoute() {
        RouteSearch routeSerch=new RouteSearch(Activity_NaviChoose.this);

        routeSerch.setRouteSearchListener(this);
        LatLonPoint start_lat=new LatLonPoint(la_start, ln_start); //定位本地座標
        LatLonPoint end_lat=new LatLonPoint(la_end,ln_end);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                start_lat, end_lat);
        //是否計算夜班車,預設為不計算。0:不計算,1:計算。可選
        BusRouteQuery query=new BusRouteQuery(fromAndTo, RouteSearch.BusDefault, "北京", 0);
        routeSerch.calculateBusRouteAsyn(query);
    }

第四步 新增路徑規劃回撥方法–重寫介面OnRouteSearchListener的方法

自駕回調

@Override
    public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
        Log.e("route", "--------------onDriveRouteSearched---------------");
        if (rCode == 0) {
            if (result != null && result.getPaths() != null
                    && result.getPaths().size() > 0) {
                DriveRouteResult driveRouteResult = result;
                DrivePath drivePath = driveRouteResult.getPaths().get(0);
                aMap.clear();// 清理地圖上的所有覆蓋物
                DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                        this, aMap, drivePath, driveRouteResult.getStartPos(),
                        driveRouteResult.getTargetPos());
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay.addToMap();
                drivingRouteOverlay.zoomToSpan();
                CodeToString(new LatLng(la_start, ln_start));
                CodeToString(new LatLng(la_end, ln_end));
            } else {
                Toast.makeText(getApplicationContext(), "對不起 沒有搜尋到相關資料", 0).show();
            }
        } else if (rCode == 27) {
            Toast.makeText(getApplicationContext(), "搜尋失敗 請檢查網路連線", 0).show();

        } else if (rCode == 32) {
            Toast.makeText(getApplicationContext(), "驗證無效", 0).show();

        } else {
            Toast.makeText(getApplicationContext()," 未知錯誤 請稍後重試 錯誤碼為"+rCode, 0).show();

        }
    }

步行回撥

@Override
    public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
        Log.e("route", "--------------onWalkRouteSearched---------------");
        if (rCode == 0) {
            if (result != null && result.getPaths() != null
                    && result.getPaths().size() > 0) {
                WalkRouteResult  walkRouteResult = result;
                WalkPath walkPath = walkRouteResult.getPaths().get(0);
                aMap.clear();// 清理地圖上的所有覆蓋物
                WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this,
                        aMap, walkPath, walkRouteResult.getStartPos(),
                        walkRouteResult.getTargetPos());
                walkRouteOverlay.removeFromMap();
                walkRouteOverlay.addToMap();
                walkRouteOverlay.zoomToSpan();
                CodeToString(new LatLng(la_start, ln_start));
                CodeToString(new LatLng(la_end, ln_end));
            } else {
                Toast.makeText(getApplicationContext(), "對不起 沒有搜尋到相關資料", 0).show();
            }
        } else if (rCode == 27) {
            Toast.makeText(getApplicationContext(), "搜尋失敗 請檢查網路連線", 0).show();
        } else if (rCode == 32) {
            Toast.makeText(getApplicationContext(), "驗證無效", 0).show();
        } else {
            Toast.makeText(getApplicationContext()," 未知錯誤 請稍後重試 錯誤碼為"+rCode, 0).show();
        }
    }

公交回調

@Override
    public void onBusRouteSearched(BusRouteResult result, int rCode) {
        Log.e("route", "--------------onBusRouteSearched---------------");
        if (rCode == 0) {
            if (result != null && result.getPaths() != null
                    && result.getPaths().size() > 0) {
                BusRouteResult busRouteResult = result;
                BusPath busPath = busRouteResult.getPaths().get(0);
                //獲取公交換乘路段列表
                List<BusStep> steps = busPath.getSteps();

                StringBuffer showBusLine = showBusLine(steps);
                String string = showBusLine.toString().substring(0, showBusLine.toString().length()-6);
                saveNaviLine(string,"gj");
                Log.e("公交線路", string);

                aMap.clear();// 清理地圖上的所有覆蓋物
                BusRouteOverlay routeOverlay = new BusRouteOverlay(this, aMap,
                        busPath,busRouteResult.getStartPos(),
                    busRouteResult.getTargetPos()
                        );
                routeOverlay.removeFromMap();
                routeOverlay.addToMap();
                routeOverlay.zoomToSpan();
            } else {
                Toast.makeText(getApplicationContext(), "對不起 沒有搜尋到相關資料", 0).show();
            }
        } else if (rCode == 27) {
            Toast.makeText(getApplicationContext(), "搜尋失敗 請檢查網路連線", 0).show();
        } else if (rCode == 32) {
            Toast.makeText(getApplicationContext(), "驗證無效", 0).show();
        } else {
            Toast.makeText(getApplicationContext()," 未知錯誤 請稍後重試 錯誤碼為"+rCode, 0).show();
        }
    }

其中公交可以獲取站點的資訊

/**
     * @param steps
     * 分析公交線路
     */
    private StringBuffer showBusLine(List<BusStep> steps) {
        sb.delete(0,sb.length());
        Log.e("公交路段數量", "----------------------"+steps.size());
        for(int i=0;i<steps.size();i++){
            BusStep busStep = steps.get(i);

            RouteBusLineItem busLine = busStep.getBusLine(); //獲取公交導航資訊

            //System.out.println("-------------------"+busLine.toString()); 地鐵1號線(四惠東--蘋果園) 05:05-23:15
            if(busLine!=null && !busLine.equals(null)){
                Log.e("公交站名稱", "--------------"+busLine.getBusLineName());
                BusStationItem arrivalBusStation = busLine.getArrivalBusStation();//到達站
                BusStationItem departureBusStation = busLine.getDepartureBusStation();//出發站

                String busStationName = arrivalBusStation.getBusStationName();//到達站公交站名稱
                String busStationName2 = departureBusStation.getBusStationName();//出發站公交站名稱

                Log.e("公交",busLine.getBusLineName()+"到達"+busStationName);
                //Log.e("站名","到達站公交站名稱"+busStationName+"出發站公交站名稱"+busStationName2);
                sb.append(busLine.getBusLineName()+"到達"+busStationName+"--換乘--");
            }else{
                Log.e("null", "------------busStep.getBusLine().equals(null)---------------------");
            }

        }

        return sb;

    }

第五步 新增自駕和步行的導航

第一步 註冊導航和語音

可以在oncreate()方法中註冊

 //語音播報開始
        TTSController ttsManager = TTSController.getInstance(this);// 初始化語音模組
        ttsManager.init();
        AMapNavi.getInstance(this).setAMapNaviListener(ttsManager);// 設定語音模組播報

        TTSController.getInstance(this).startSpeaking();

        //導航註冊
        AMapNavi.getInstance(this).setAMapNaviListener(this);

第二部 開啟導航的方法

/**
     * 開啟導航的方法
     */
    public void StartNavi(int i) {
        Log.e("navi", "-----------StartNavi-------------");
        //起點終點
         NaviLatLng mNaviStart = new NaviLatLng(la_start, ln_start);
         NaviLatLng mNaviEnd = new NaviLatLng(la_end,ln_end);
        //起點終點列表
         ArrayList<NaviLatLng> mStartPoints = new ArrayList<NaviLatLng>();
         ArrayList<NaviLatLng> mEndPoints = new ArrayList<NaviLatLng>();
         mStartPoints.add(mNaviStart);
         mEndPoints.add(mNaviEnd);

         if(i==1){  //自駕    起點  終點  途經點  模式:最快  最省錢等
             AMapNavi.getInstance(this).setAMapNaviListener(this);
             AMapNavi.getInstance(this).calculateDriveRoute(mStartPoints,
                        mEndPoints, null, AMapNavi.DrivingDefault);
         }else{  //步行
             AMapNavi.getInstance(this).calculateWalkRoute(mNaviStart, mNaviEnd); 
         }



    }

第三步 導航的回撥,重寫導航介面AMapNaviListener的方法

其中這個方法標誌著導航成功呼叫導航頁面

@Override
    public void onCalculateRouteSuccess() {
        Log.e("Navi", "-------onCalculateRouteSuccess--------------------");

        Intent intent = new Intent(Activity_NaviChoose.this,
                SimpleNaviActivity.class);  //導航頁面
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        Bundle bundle=new Bundle();
        bundle.putInt(Utils.ACTIVITYINDEX, Utils.SIMPLEGPSNAVI);
        bundle.putBoolean(Utils.ISEMULATOR, false);
        intent.putExtras(bundle);
        startActivity(intent);

    }

新增導航頁面佈局及相關引數

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <com.amap.api.navi.AMapNaviView
        android:id="@+id/simplenavimap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</LinearLayout>
<string name="preference_default_tts_role">xiaoyan</string>
    <string name="preference_key_tts_speed">tts_speed</string>
    <string name="preference_title_tts_speed">語速</string>
    <string name="preference_default_tts_speed">50</string>
    <string name="preference_dialog_title_tts_speed">選擇語速</string>
    <string name="preference_key_tts_volume">tts_volume</string>
    <string name="preference_title_tts_volume">音量</string>
    <string name="preference_default_tts_volume">50</string>
    <string name="preference_dialog_title_tts_volume">選擇音量</string>
    <string name="preference_key_tts_pitch">tts_pitch</string>
    <string name="preference_title_tts_pitch">語調</string>
    <string name="preference_default_tts_pitch">50</string>
      <string name="app_id">5613985e</string>

最後處理一下導航頁面類的返回按鈕跳轉回哪個頁面即可