本节将要实现的功能是路线导航,确定出发点和到达的终点坐标,两点之间的路线导航。跟前几篇一样,上代码
首先是类文件NaviActivity.java
package com.junto.gdmaptest.navi;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.junto.gdmaptest.R;
import com.junto.gdmaptest.util.ErrorInfo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by WangJinyong on 2018/11/7.
* 路线导航
*/
public class NaviActivity extends Activity implements AMapNaviListener,AMapNaviViewListener {
protected AMapNaviView mAMapNaviView;
protected AMapNavi mAMapNavi;
protected NaviLatLng mEndLatlng = new NaviLatLng(40.084894,116.603039);//终点经纬度
protected NaviLatLng mStartLatlng = new NaviLatLng(39.825934,116.342972);//起点经纬度
protected final List<NaviLatLng> sList = new ArrayList<NaviLatLng>();//起点经纬度集合
protected final List<NaviLatLng> eList = new ArrayList<NaviLatLng>();//终点经纬度集合
protected List<NaviLatLng> mWayPointList;//途经点经纬度集合
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_navi);
mAMapNaviView = findViewById(R.id.navi_view);
mAMapNaviView.onCreate(savedInstanceState);
mAMapNaviView.setAMapNaviViewListener(this);
mAMapNavi = AMapNavi.getInstance(getApplicationContext());
mAMapNavi.addAMapNaviListener(this);
mAMapNavi.setUseInnerVoice(true);//使用内置语音播报
//设置模拟导航的行车速度
mAMapNavi.setEmulatorNaviSpeed(75);
sList.add(mStartLatlng);
eList.add(mEndLatlng);
mWayPointList = new ArrayList();
mWayPointList.add(new NaviLatLng(39.925846, 116.442765));
}
@Override
protected void onResume() {
super.onResume();
mAMapNaviView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mAMapNaviView.onPause();
// 停止导航之后,会触及底层stop,然后就不会再有回调了,但是讯飞当前还是没有说完的半句话还是会说完
// mAMapNavi.stopNavi();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAMapNaviView.onDestroy();
//since 1.6.0 不再在naviview destroy的时候自动执行AMapNavi.stopNavi();请自行执行
mAMapNavi.stopNavi();
mAMapNavi.destroy();
}
//==============================下面是AMapNaviListener实现的方法=========================//
@Override
public void onInitNaviFailure() {
}
@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, true, true, false, true);
} catch (Exception e) {
e.printStackTrace();
}
mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy);//起始点坐标、终点坐标、途经点坐标、strategy
// mAMapNavi.calculateWalkRoute(new NaviLatLng(39.925846, 116.435765), new NaviLatLng(39.925846, 116.532765));//步行导航
// mAMapNavi.calculateRideRoute(new NaviLatLng(39.925846, 116.435765), new NaviLatLng(39.925846, 116.532765));//骑行导航
}
@Override
public void onCalculateRouteSuccess(int[] ints) {
//多路径算路成功回调
// mAMapNavi.startNavi(NaviType.EMULATOR);//模拟导航
mAMapNavi.startNavi(NaviType.GPS);//实时导航
}
@Override
public void onStartNavi(int i) {
//开始导航回调
}
@Override
public void onTrafficStatusUpdate() {
}
@Override
public void onLocationChange(AMapNaviLocation aMapNaviLocation) {
//当前位置回调
}
@Override
public void onGetNavigationText(int i, String s) {
//播报类型和播报文字回调
}
@Override
public void onGetNavigationText(String s) {
}
@Override
public void onEndEmulatorNavi() {
//结束模拟导航
}
@Override
public void onArriveDestination() {
//到达目的地
}
@Override
public void onCalculateRouteFailure(int errorInfo) {
//路线计算失败
Log.e("dm", "--------------------------------------------");
Log.i("dm", "路线计算失败:错误码=" + errorInfo + ",Error Message= " + ErrorInfo.getError(errorInfo));
Log.i("dm", "错误码详细链接见:http://lbs.amap.com/api/android-navi-sdk/guide/tools/errorcode/");
Log.e("dm", "--------------------------------------------");
Toast.makeText(this, "errorInfo:" + errorInfo + ",Message:" + ErrorInfo.getError(errorInfo), Toast.LENGTH_LONG).show();
}
@Override
public void onReCalculateRouteForYaw() {
//偏航后重新计算路线回调
}
@Override
public void onReCalculateRouteForTrafficJam() {
//拥堵后重新计算路线回调
}
@Override
public void onArrivedWayPoint(int i) {
//到达途径点
}
@Override
public void onGpsOpenStatus(boolean b) {
//GPS开关状态回调
}
@Override
public void onNaviInfoUpdate(NaviInfo naviInfo) {
//导航过程中的信息更新,请看NaviInfo的具体说明
}
@Override
public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {
//过时
}
@Override
public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {
}
@Override
public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {
}
@Override
public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {
}
@Override
public void showCross(AMapNaviCross aMapNaviCross) {
//显示转弯回调
}
@Override
public void hideCross() {
//隐藏转弯回调
}
@Override
public void showModeCross(AMapModelCross aMapModelCross) {
}
@Override
public void hideModeCross() {
}
@Override
public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {
}
@Override
public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {
//显示车道信息
}
@Override
public void hideLaneInfo() {
//隐藏车道信息
}
@Override
public void notifyParallelRoad(int i) {
if (i == 0) {
Toast.makeText(this, "当前在主辅路过渡", Toast.LENGTH_SHORT).show();
Log.d("wlx", "当前在主辅路过渡");
return;
}
if (i == 1) {
Toast.makeText(this, "当前在主路", Toast.LENGTH_SHORT).show();
Log.d("wlx", "当前在主路");
return;
}
if (i == 2) {
Toast.makeText(this, "当前在辅路", Toast.LENGTH_SHORT).show();
Log.d("wlx", "当前在辅路");
}
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
//已过时
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
//更新交通设施信息
}
@Override
public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
//已过时
}
@Override
public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
//更新巡航模式的统计信息
}
@Override
public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
//更新巡航模式的拥堵信息
}
@Override
public void onPlayRing(int i) {
}
@Override
public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {
}
@Override
public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {
}
@Override
public void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {
}
//============================下面是AMapNaviViewListener实现的方法==================================//
@Override
public void onNaviSetting() {
//底部导航设置点击回调
}
@Override
public void onNaviCancel() {
finish();
}
@Override
public boolean onNaviBackClick() {
return false;
}
@Override
public void onNaviMapMode(int i) {
//地图的模式,锁屏或锁车
}
@Override
public void onNaviTurnClick() {
//转弯view的点击回调
}
@Override
public void onNextRoadClick() {
//下一个道路View点击回调
}
@Override
public void onScanViewButtonClick() {
//全览按钮点击回调
}
@Override
public void onLockMap(boolean b) {
//锁地图状态发生变化时回调
}
@Override
public void onNaviViewLoaded() {
Log.d("wlx", "导航页面加载成功");
Log.d("wlx", "请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");
}
@Override
public void onMapTypeChanged(int i) {
}
@Override
public void onNaviViewShowMode(int i) {
}
}
然后是布局文件activity_navi.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="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"
android:layout_weight="1"/>
</LinearLayout>
其中有实时导航和模拟导航
@Override
public void onCalculateRouteSuccess(int[] ints) {
//多路径算路成功回调
// mAMapNavi.startNavi(NaviType.EMULATOR);//模拟导航
mAMapNavi.startNavi(NaviType.GPS);//实时导航
}
步行导航和骑行导航与驾车导航就更改一句话就可以了
@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, true, true, false, true);
} catch (Exception e) {
e.printStackTrace();
}
mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy);//起始点坐标、终点坐标、途经点坐标、strategy
// mAMapNavi.calculateWalkRoute(new NaviLatLng(39.925846, 116.435765), new NaviLatLng(39.925846, 116.532765));//步行导航
// mAMapNavi.calculateRideRoute(new NaviLatLng(39.925846, 116.435765), new NaviLatLng(39.925846, 116.532765));//骑行导航
}
以上就实现了路线导航功能,实际开发我们需要根据实际需求来更改
版权声明:本文为u013184970原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。