当前位置:   article > 正文

android开发百度地图步行导航_android开发百度地图导航

android开发百度地图导航
  1. 准备工作: 在百度地图上下载依赖,配置key。
  2. 创建一个demo项目,可显示百度地图的
  3. 添加按钮,点击按钮,开始导航
  4. 发起算路我用的是routePlanWithRouteNode这个方法,官网中routePlanWithParams这个方法,我调着闪退,目前不清楚原因
  5. 这个版本的需要设置隐私政策 SDKInitializer.setAgreePrivacy(this,true);
		// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
        SDKInitializer.setAgreePrivacy(this,true);
        // 默认本地个性化地图初始化方法
        SDKInitializer.initialize(Utils.getContext());

        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 此时地图会显示比例尺,显示纯绿色 如果用了LocationClient,也给他设置一下
LocationClient.setAgreePrivacy(true);
  • 1
  1. 在mainAcitivity中,添加如下代码
	private WalkNavigateHelper helper = null;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initPermissions();  // 初始化权限
        setContentView(R.layout.activity_main); // 填充视图
        initView();  // 初始化地图
     
		// 点击按钮开始导航
        ImageButton imageButton = findViewById(R.id.nav_btn);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 初始化步行导航  
                startBikeNavi();
            }
        });
    }
	// 初始化权限
 	private void initPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] permissions = {
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
            };

            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm);
                    // 进入到这里代表没有权限.
                }
                if (!permissionsList.isEmpty()) {
                    String[] strings = new String[permissionsList.size()];
                    requestPermissions(permissionsList.toArray(strings), 0);
                }
            }

        }
    }

	/**
     * @Description: 引擎初始化
     * @Author: LY
     */
    private void startBikeNavi() {
        Log.d("=========", "startBikeNavi");
        try {
            helper = WalkNavigateHelper.getInstance();
            helper.initNaviEngine(this, new IWEngineInitListener() {
                @Override
                public void engineInitSuccess() {
                    Log.d("=========", "BikeNavi engineInitSuccess引擎初始化成功");
                    startWebNavi();
                }

                @Override
                public void engineInitFail() {
                    Log.d("=========", "BikeNavi engineInitFail");
                    BikeNavigateHelper.getInstance().unInitNaviEngine();
                }
            });
        } catch (Exception e) {
            Log.d("=========", "startBikeNavi Exception");
            e.printStackTrace();
        }
    }


    /**
     * @Description: 启动百度地图导航(Web)
     * @Author: LY
     */
    private void startWebNavi() {
        //起终点位置
        LatLng walkStartNode = new LatLng(40.047416, 116.312143);
        LatLng walkEndNode = new LatLng(40.048424, 116.313513);

        WalkRouteNodeInfo startNode = new WalkRouteNodeInfo();
        startNode.setLocation(walkStartNode);
        WalkRouteNodeInfo endNode = new WalkRouteNodeInfo();
        endNode.setLocation(walkEndNode);
        // 官网
        //构造WalkNaviLaunchParam
        WalkNaviLaunchParam walkParam = new 	WalkNaviLaunchParam().startNodeInfo(startNode).endNodeInfo(endNode);
        //获取WalkNavigateHelper实例 //发起算路
        helper.routePlanWithRouteNode(walkParam, new IWRoutePlanListener() {
            @Override
            public void onRoutePlanStart() {
                Log.d("=======", "WalkNavi onRoutePlanStart开始步行导航");
            }

            @Override
            public void onRoutePlanSuccess() {
                Log.d("========", "onRoutePlanSuccess算路成功");
                Intent intent = new Intent();
                intent.setClass(getContext(), WNaviGuideActivity.class);
                startActivity(intent);
            }

            @Override
            public void onRoutePlanFail(WalkRoutePlanError error) {
                Log.d("Walk", "WalkNavi onRoutePlanFail");
            }

        });      
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  1. 创建WNaviGuideActivity类,改包名,无脑粘
package com.example.locationapp;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.baidu.mapapi.walknavi.WalkNavigateHelper;
import com.baidu.mapapi.walknavi.adapter.IWNaviStatusListener;
import com.baidu.mapapi.walknavi.adapter.IWRouteGuidanceListener;
import com.baidu.mapapi.walknavi.adapter.IWTTSPlayer;
import com.baidu.mapapi.walknavi.model.RouteGuideKind;
import com.baidu.platform.comapi.walknavi.WalkNaviModeSwitchListener;
import com.baidu.platform.comapi.walknavi.widget.ArCameraView;

public class WNaviGuideActivity extends Activity {
    private final static String TAG = WNaviGuideActivity.class.getSimpleName();

    private WalkNavigateHelper mNaviHelper;

    private boolean isPreSPEAKtotal = true;
    private String orient = "";

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNaviHelper.quit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mNaviHelper.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mNaviHelper.pause();
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x001) {
                ChangeState();
                handler.sendEmptyMessageDelayed(0x001, 45000);
            }
            if (msg.what == 0x002) {
                handler.sendEmptyMessageDelayed(0x002, 30000);
            }
            if (msg.what == 0x003) {

                startActivity(new Intent(WNaviGuideActivity.this, MainActivity.class));
            }
        }
    };

    private void ChangeState() {
        isPreSPEAKtotal = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNaviHelper = WalkNavigateHelper.getInstance();
        try {
            View view = mNaviHelper.onCreate(WNaviGuideActivity.this);
            if (view != null) {
                setContentView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置步行导航状态监听
        mNaviHelper.setWalkNaviStatusListener(new IWNaviStatusListener() {
            @Override
            public void onWalkNaviModeChange(int mode, WalkNaviModeSwitchListener listener) {
                Log.d("======", "onWalkNaviModeChange : " + mode);
                mNaviHelper.switchWalkNaviMode(WNaviGuideActivity.this, mode, listener);
            }

            /* @Description: 这个是在退出导航时自动调用的方法,在这里要把对象进行释放,避免空对象的产生
             * @Author: LiY                                                                                                                                                                         ue
             */
            @Override
            public void onNaviExit() {
                Log.d("======", "onNaviExit");
                handler.removeMessages(0x001);
                handler.removeMessages(0x002);
                handler.removeMessages(0x003);
            }
        });

        /**
         * 诱导文本回调
         * @param s 诱导文本
         * @param b 是否抢先播报
         * @return
         */
        mNaviHelper.setTTsPlayer(new IWTTSPlayer() {
            @Override
            public int playTTSText(final String s, boolean b) {
                Log.d(TAG, "tts: " + s);
                return 0;
            }
        });


        boolean startResult = mNaviHelper.startWalkNavi(WNaviGuideActivity.this);
        Log.e(TAG, "startWalkNavi result : " + startResult);
        //设置路线指引监听
        mNaviHelper.setRouteGuidanceListener(this, new

                IWRouteGuidanceListener() {
                    /**
                     * @Description: 诱导图标更改方法回调
                     * @Author: LY
                     */
                    @Override
                    public void onRouteGuideIconUpdate(Drawable icon) {

                    }

                    @Override
                    public void onRouteGuideKind(RouteGuideKind routeGuideKind) {
                        Log.d("======", "onRouteGuideKind: " + routeGuideKind);
                        if (routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_PassRoad_Left || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_PassRoad_Right || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_Right_PassRoad_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_Right_PassRoad_UTurn)

                            if (routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Left || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Left_Front || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right_Back || routeGuideKind == RouteGuideKind.NE_Maneuver_Kind_RightDiagonal_PassRoad_Right_Front){
                                Log.d("========", "onRouteGuideKind: aaaaaa");
                            }

                    }

                    /**
                     * @Description: 诱导信息
                     */
                    @Override
                    public void onRoadGuideTextUpdate(CharSequence charSequence, CharSequence
                            charSequence1) {
                        Log.d(TAG, "onRoadGuideTextUpdate   charSequence=: " + charSequence + "   charSequence1 = : " +
                                charSequence1);
                        orient = charSequence.toString() + charSequence1.toString();

                    }

                    /**
                     *
                     * @Description: 总剩余距离回调
                     */
                    @Override
                    public void onRemainDistanceUpdate(CharSequence charSequence) {
                        Log.d(TAG, "onRemainDistanceUpdate: charSequence = :" + charSequence);
                        if (isPreSPEAKtotal) {
                        }
                    }

                    /**
                     *
                     * @Description: 总剩余时间回调
                     * @Author: LY
                     */
                    @Override
                    public void onRemainTimeUpdate(CharSequence charSequence) {
                        Log.d(TAG, "onRemainTimeUpdate: charSequence = :" + charSequence);
                        if (isPreSPEAKtotal) {
                            isPreSPEAKtotal = false;
                        }
                    }


                    @Override
                    public void onGpsStatusChange(CharSequence charSequence, Drawable drawable) {
                        Log.d(TAG, "onGpsStatusChange: charSequence = :" + charSequence);

                    }


                    @Override
                    public void onRouteFarAway(CharSequence charSequence, Drawable drawable) {
                        Log.d(TAG, "onRouteFarAway: charSequence = :" + charSequence);
                    }

                    /**
                     * @Description: 偏航规划中
                     */
                    @Override
                    public void onRoutePlanYawing(CharSequence charSequence, Drawable drawable) {
                        Log.d(TAG, "onRoutePlanYawing: charSequence = :" + charSequence);

                    }

                    /**
                     *
                     * @Description: 重新算路成功
                     */
                    @Override
                    public void onReRouteComplete() {
                    }

                    /**
                     *
                     * @Description: 到达目的地回调
                     */
                    @Override
                    public void onArriveDest() {
                        handler.sendEmptyMessageDelayed(0x003, 6000);
                    }

                    @Override
                    public void onIndoorEnd(Message msg) {

                    }

                    @Override
                    public void onFinalEnd(Message msg) {

                    }

                    @Override
                    public void onVibrate() {

                    }
                });
        handler.sendEmptyMessage(0x001);
        handler.sendEmptyMessage(0x002);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == ArCameraView.WALK_AR_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(WNaviGuideActivity.this, "没有相机权限,请打开后重试", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mNaviHelper.startCameraAndSetMapView(WNaviGuideActivity.this);
            }
        }
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  1. 在AndroidManifest中声明WNaviGuideActivity
<activity android:name=".WNaviGuideActivity">

        </activity>
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/297627
推荐阅读
相关标签
  

闽ICP备14008679号