主要代码:
public class MainActivity extends BaseActivity {
private EditText edit_end_lng;
private EditText edit_end_lat;
private EditText edi_star_Lat;
private EditText edi_star_Lng;
public static Double endLng = 0.0;
public static Double endLat = 0.0;
public static Double startLat = 0.0;
public static Double startLng = 0.0;
NaviLatLng mStartLatlng ;
NaviLatLng mEndLatlng ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniUi();
mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
mAMapNaviView.onCreate(savedInstanceState);
mAMapNaviView.setAMapNaviViewListener(this);
}
public void iniUi()
{
edit_end_lat = (EditText) findViewById(R.id.edit_end_lat);
edit_end_lng = (EditText) findViewById(R.id.edit_end_lng);
edi_star_Lat = (EditText) findViewById(R.id.edi_star_Lat);
edi_star_Lng = (EditText) findViewById(R.id.edi_star_Lng);
}
public void btnSure(View v)
{
String end_lat = edit_end_lat.getText().toString();
String end_lng = edit_end_lng.getText().toString();
String star_lat = edi_star_Lat.getText().toString();
String star_lng = edi_star_Lng.getText().toString();
if (end_lng != null) {
try {
endLng = Double.parseDouble(end_lng);
} catch (Exception e) {
}
}
if (end_lat != null) {
try {
endLat = Double.parseDouble(end_lat);
} catch (Exception e) {
}
}
if (star_lat != null) {
try {
startLat = Double.parseDouble(star_lat);
} catch (Exception e) {
}
}
if (star_lng != null) {
try {
startLng = Double.parseDouble(star_lng);
} catch (Exception e) {
}
}
Log.e("日志查看","endLng="+endLng );
Log.e("日志查看","endLat="+endLat );
Log.e("日志查看","startLat="+startLat);
Log.e("日志查看","startLng="+startLng );
mStartLatlng = new NaviLatLng(startLng,startLat);
mEndLatlng = new NaviLatLng(endLng,endLat);
//文本框中输入正常的经纬度就可以导航成功,如下列所示:
//开始输入:经度24.67, 纬度100.10
//结束输入:经度25.80, 纬度118.34
// 设置模拟导航的行车速度
mAMapNavi.setEmulatorNaviSpeed(350);
sList.add(mStartLatlng);
eList.add(mEndLatlng);
onInitNaviSuccess();
}
}
public class BaseActivity extends Activity implements AMapNaviListener, AMapNaviViewListener {
protected AMapNaviView mAMapNaviView;
protected AMapNavi mAMapNavi;
protected TTSController mTtsManager;
protected final List<NaviLatLng> sList = new ArrayList<NaviLatLng>();
protected final List<NaviLatLng> eList = new ArrayList<NaviLatLng>();
protected List<NaviLatLng> mWayPointList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//实例化语音引擎
mTtsManager = TTSController.getInstance(getApplicationContext());
mTtsManager.init();
mTtsManager.startSpeaking();
//
mAMapNavi = AMapNavi.getInstance(getApplicationContext());
mAMapNavi.addAMapNaviListener(this);
mAMapNavi.addAMapNaviListener(mTtsManager);
/* 设置模拟导航的行车速度
mAMapNavi.setEmulatorNaviSpeed(350);
sList.add(new MainActivity().mStartLatlng);
eList.add(new MainActivity().mEndLatlng);
sList.add(mStartLatlng);
eList.add(mEndLatlng);*/
}
@Override
protected void onResume() {
super.onResume();
mAMapNaviView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mAMapNaviView.onPause();
// 仅仅是停止你当前在说的这句话,一会到新的路口还是会再说的
mTtsManager.stopSpeaking();
//
// 停止导航之后,会触及底层stop,然后就不会再有回调了,但是讯飞当前还是没有说完的半句话还是会说完
// mAMapNavi.stopNavi();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAMapNaviView.onDestroy();
//since 1.6.0 不再在naviview destroy的时候自动执行AMapNavi.stopNavi();请自行执行
mAMapNavi.stopNavi();
mAMapNavi.destroy();
mTtsManager.destroy();
}
@Override
public void onInitNaviFailure() {
Toast.makeText(this, "init navi Failed", Toast.LENGTH_SHORT).show();
}
@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 {
strategy=mAMapNavi.strategyConvert(true, false, false, false, false);
} catch (Exception e) {
e.printStackTrace();
}
mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList,strategy);
}
@Override
public void onStartNavi(int type) {
}
@Override
public void onTrafficStatusUpdate() {
}
@Override
public void onLocationChange(AMapNaviLocation location) {
}
@Override
public void onGetNavigationText(int type, String text) {
}
@Override
public void onEndEmulatorNavi() {
}
@Override
public void onArriveDestination() {
}
@Override
public void onCalculateRouteSuccess() {
mAMapNavi.startNavi(NaviType.EMULATOR);
}
@Override
public void onCalculateRouteFailure(int errorInfo) {
}
@Override
public void onReCalculateRouteForYaw() {
}
@Override
public void onReCalculateRouteForTrafficJam() {
}
@Override
public void onArrivedWayPoint(int wayID) {
}
@Override
public void onGpsOpenStatus(boolean enabled) {
}
@Override
public void onNaviSetting() {
}
@Override
public void onNaviMapMode(int isLock) {
}
@Override
public void onNaviCancel() {
finish();
}
@Override
public void onNaviTurnClick() {
}
@Override
public void onNextRoadClick() {
}
@Override
public void onScanViewButtonClick() {
}
@Deprecated
@Override
public void onNaviInfoUpdated(AMapNaviInfo naviInfo) {
}
@Override
public void onNaviInfoUpdate(NaviInfo naviinfo) {
}
@Override
public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
}
@Override
public void showCross(AMapNaviCross aMapNaviCross) {
}
@Override
public void hideCross() {
}
@Override
public void showLaneInfo(AMapLaneInfo[] laneInfos, byte[] laneBackgroundInfo, byte[] laneRecommendedInfo) {
}
@Override
public void hideLaneInfo() {
}
@Override
public void onCalculateMultipleRoutesSuccess(int[] ints) {
}
@Override
public void notifyParallelRoad(int i) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
}
@Override
public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
}
@Override
public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
}
@Override
public void onLockMap(boolean isLock) {
}
@Override
public void onNaviViewLoaded() {
Log.d("wlx", "导航页面加载成功");
Log.d("wlx", "请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");
}
@Override
public boolean onNaviBackClick() {
return false;
}
}
public class TTSController implements SynthesizerListener, AMapNaviListener {
public static TTSController ttsManager;
boolean isfinish = true;
private Context mContext;
// 合成对象.
private SpeechSynthesizer mSpeechSynthesizer;
/**
* 用户登录回调监听器.
*/
private SpeechListener listener = new SpeechListener() {
@Override
public void onData(byte[] arg0) {
}
@Override
public void onCompleted(SpeechError error) {
if (error != null) {
}
}
@Override
public void onEvent(int arg0, Bundle arg1) {
}
};
TTSController(Context context) {
mContext = context;
}
public static TTSController getInstance(Context context) {
if (ttsManager == null) {
ttsManager = new TTSController(context);
}
return ttsManager;
}
public void init() {
SpeechUser.getUser().login(mContext, null, null,
"appid=" + mContext.getString(R.string.app_id), listener);
// 初始化合成对象.
mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(mContext);
initSpeechSynthesizer();
}
/**
* 使用SpeechSynthesizer合成语音,不弹出合成Dialog.
*
* @param
*/
public void playText(String playText) {
if (!isfinish) {
return;
}
if (null == mSpeechSynthesizer) {
// 创建合成对象.
mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(mContext);
initSpeechSynthesizer();
}
// 进行语音合成.
mSpeechSynthesizer.startSpeaking(playText, this);
}
public void stopSpeaking() {
if (mSpeechSynthesizer != null)
mSpeechSynthesizer.stopSpeaking();
}
public void startSpeaking() {
isfinish = true;
}
private void initSpeechSynthesizer() {
// 设置发音人
mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME,
mContext.getString(R.string.preference_default_tts_role));
// 设置语速
mSpeechSynthesizer.setParameter(SpeechConstant.SPEED,
"" + mContext.getString(R.string.preference_key_tts_speed));
// 设置音量
mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME,
"" + mContext.getString(R.string.preference_key_tts_volume));
// 设置语调
mSpeechSynthesizer.setParameter(SpeechConstant.PITCH,
"" + mContext.getString(R.string.preference_key_tts_pitch));
}
@Override
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
}
@Override
public void onCompleted(SpeechError arg0) {
isfinish = true;
}
@Override
public void onSpeakBegin() {
isfinish = false;
}
@Override
public void onSpeakPaused() {
}
@Override
public void onSpeakProgress(int arg0, int arg1, int arg2) {
}
@Override
public void onSpeakResumed() {
}
public void destroy() {
if (mSpeechSynthesizer != null) {
mSpeechSynthesizer.stopSpeaking();
}
}
@Override
public void onArriveDestination() {
this.playText("到达目的地");
}
@Override
public void onArrivedWayPoint(int arg0) {
}
@Override
public void onCalculateRouteFailure(int arg0) {
this.playText("路径计算失败,请检查网络或输入参数");
}
@Override
public void onCalculateRouteSuccess() {
String calculateResult = "路径计算就绪";
this.playText(calculateResult);
}
@Override
public void onEndEmulatorNavi() {
this.playText("导航结束");
}
@Override
public void onGetNavigationText(int arg0, String arg1) {
this.playText(arg1);
}
@Override
public void onInitNaviFailure() {
}
@Override
public void onInitNaviSuccess() {
}
@Override
public void onLocationChange(AMapNaviLocation arg0) {
}
@Override
public void onReCalculateRouteForTrafficJam() {
this.playText("前方路线拥堵,路线重新规划");
}
@Override
public void onReCalculateRouteForYaw() {
this.playText("您已偏航");
}
@Override
public void onStartNavi(int arg0) {
}
@Override
public void onTrafficStatusUpdate() {
}
@Override
public void onGpsOpenStatus(boolean arg0) {
}
@Override
public void onNaviInfoUpdated(AMapNaviInfo arg0) {
}
@Override
public void onNaviInfoUpdate(NaviInfo arg0) {
}
@Override
public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
}
@Override
public void showCross(AMapNaviCross aMapNaviCross) {
}
@Override
public void hideCross() {
}
@Override
public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {
}
@Override
public void hideLaneInfo() {
}
@Override
public void onCalculateMultipleRoutesSuccess(int[] ints) {
}
@Override
public void notifyParallelRoad(int i) {
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
}
@Override
public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
}
@Override
public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
}
}
public class Utils {
public static final String DAY_NIGHT_MODE = "daynightmode";
public static final String DEVIATION = "deviationrecalculation";
public static final String JAM = "jamrecalculation";
public static final String TRAFFIC = "trafficbroadcast";
public static final String CAMERA = "camerabroadcast";
public static final String SCREEN = "screenon";
public static final String THEME = "theme";
public static final String ISEMULATOR = "isemulator";
public static final String ACTIVITYINDEX = "activityindex";
public static final int SIMPLEHUDNAVIE = 0;
public static final int EMULATORNAVI = 1;
public static final int SIMPLEGPSNAVI = 2;
public static final int SIMPLEROUTENAVI = 3;
public static final boolean DAY_MODE = false;
public static final boolean NIGHT_MODE = true;
public static final boolean YES_MODE = true;
public static final boolean NO_MODE = false;
public static final boolean OPEN_MODE = true;
public static final boolean CLOSE_MODE = false;
}