高德 开发 Android 导航SDK 概述
更新时间:2018年11月22日
简介
Android 导航 SDK 是一款针对在线导航的产品,产品功能涵盖路径规划、模拟导航、GPS 定位、自定义导航界面、获取导航播报信息等。此外,该产品的导航路径计算与实时交通信息相结合,力求为用户提供更加合理、准确、人性化的导航服务。
- 模拟导航
//算路终点坐标 protected NaviLatLng mEndLatlng = new NaviLatLng(22.652, 113.966); //算路起点坐标 protected NaviLatLng mStartLatlng = new NaviLatLng(22.540332, 113.939961); //存储算路起点的列表 protected final List<NaviLatLng> sList = new ArrayList<NaviLatLng>(); //存储算路终点的列表 protected final List<NaviLatLng> eList = new ArrayList<NaviLatLng>(); //实现AMapNaviView生命周期 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取AMapNavi实例 mAMapNavi = AMapNavi.getInstance(getApplicationContext()); //添加监听 mAMapNavi.addAMapNaviListener(this); mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view); mAMapNaviView.setAMapNaviViewListener(this); //设置模拟导航的行车速度 mAMapNavi.setEmulatorNaviSpeed(75); sList.add(mStartLatlng); eList.add(mEndLatlng); } @Override protected void onResume() { super.onResume(); mAMapNaviView.onResume(); } @Override protected void onPause() { super.onPause(); mAMapNaviView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mAMapNaviView.onDestroy(); } //导航初始化成功,进行路径规划 @Override public void onInitNaviSuccess() { /** * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); 参数: * * @congestion 躲避拥堵 * @avoidhightspeed 不走高速 * @cost 避免收费 * @hightspeed 高速优先 * @multipleroute 多路径 * * 说明: 以上参数都是boolean类型,其中multipleroute参数表示是否多条路线,如果为true则此策略会算出多条路线。 * 注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true */ int strategy = 0; try { //再次强调,最后一个参数为true时代表多路径,否则代表单路径 strategy = mAMapNavi.strategyConvert(true, false, false, false, false); } catch (Exception e) { e.printStackTrace(); } // 驾车算路 mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy); } //算路成功回调,开始导航 @Override public void onCalculateRouteSuccess() { mAMapNavi.startNavi(NaviType.EMULATOR); }
- HUD导航
//实现AMapHudView生命周期 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取AMapNavi实例 mAMapNavi = AMapNavi.getInstance(getApplicationContext()); //添加监听 mAMapNavi.addAMapNaviListener(this); mAMapHudView = (AMapHudView) findViewById(R.id.hudview); mAMapHudView.setHudViewListener(this); } @Override protected void onResume() { super.onResume(); mAMapHudView.onResume(); } @Override protected void onPause() { super.onPause(); mAMapHudView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mAMapHudView.onDestroy(); } //导航初始化成功,进行路径规划 @Override public void onInitNaviSuccess() { /** * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); 参数: * * @congestion 躲避拥堵 * @avoidhightspeed 不走高速 * @cost 避免收费 * @hightspeed 高速优先 * @multipleroute 多路径 * * 说明: 以上参数都是boolean类型,其中multipleroute参数表示是否多条路线,如果为true则此策略会算出多条路线。 * 注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true */ int strategy = 0; try { //再次强调,最后一个参数为true时代表多路径,否则代表单路径 strategy = mAMapNavi.strategyConvert(true, false, false, false, false); } catch (Exception e) { e.printStackTrace(); } mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy); } //算路成功回调,开始导航 @Override public void onCalculateRouteSuccess() { mAMapNavi.startNavi(NaviType.EMULATOR); }
- 多路线规划
/* * strategyFlag转换出来的值都对应PathPlanningStrategy常量,用户也可以直接传入PathPlanningStrategy常量进行算路。 * 如:mAMapNavi.calculateDriveRoute(mStartList, mEndList, mWayPointList,PathPlanningStrategy.DRIVING_DEFAULT); */ int strategyFlag = 0; try { /** * 方法: * int strategy = mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); * 参数: * @congestion 躲避拥堵 * @avoidhightspeed 不走高速 * @cost 避免收费 * @hightspeed 高速优先 * @multipleroute 多路径 * * 说明: * 以上参数都是boolean类型,其中multipleroute参数表示是否多条路线,如果为true则此策略会算出多条路线。 * 注意: * 不走高速与高速优先不能同时为true * 高速优先与避免收费不能同时为true */ strategyFlag = mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, true); } catch (Exception e) { e.printStackTrace(); } //多路径计算成功后,进入onCalculateMultipleRoutesSuccess 回调 @Override public void onCalculateMultipleRoutesSuccess(int[] ints) { //清空上次计算的路径列表。 routeOverlays.clear(); HashMap<Integer, AMapNaviPath> paths = mAMapNavi.getNaviPaths(); for (int i = 0; i < ints.length; i++) { AMapNaviPath path = paths.get(ints[i]); if (path != null) { drawRoutes(ints[i], path); } } } //对于多路径需要告诉 AMapNavi 选择的是哪条路,然后才能进行导航 aMapNavi.selectRouteId(routeIds[routeIndex]);
- 自定义路口放大图
//定义导航界面View和路口放大图控件View <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.amap.api.navi.AMapNaviView android:id="@+id/navi_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.amap.api.navi.view.ZoomInIntersectionView android:id="@+id/myZoomInIntersectionView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout> //实现路口放大图自定义显示 mZoomInIntersectionView = (ZoomInIntersectionView) findViewById(R.id.myZoomInIntersectionView); mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view); mAMapNaviView.onCreate(savedInstanceState); //设置布局完全不可见 AMapNaviViewOptions options = mAMapNaviView.getViewOptions(); options.setLayoutVisible(false); mAMapNaviView.setViewOptions(options); mAMapNaviView.setLazyZoomInIntersectionView(mZoomInIntersectionView);
- 外部传入GPS数据
//使用外部GPS数据 mAMapNavi.setIsUseExtraGPSData(true); //设置外部GPS数据 Location location = new Location("gps仪器型号"); location.setLongitude(116.4 - 0.01 * i); location.setLatitude(39.9); location.setSpeed(5); location.setAccuracy(1); location.setBearing(5); location.setTime(System.currentTimeMillis()); //以上6项数据缺一不可 mAMapNavi.setExtraGPSData(location);
给开发者的建议
高德开放平台提供的地图API/SDK,任何非盈利性网站/应用均可免费使用 。请参阅《高德地图 API 使用条款》获得详细信息。