package com.jiuqi.njt.ui.route;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.integer;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.location.Location;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.CancelableCallback;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.search.core.LatLonPoint;
import com.amap.api.search.route.Route;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.ui.SimpleNaviActivity;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.TextToSpeechUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.widget.ProgressDialogStyle;
import com.jqyd.android.module.lbs.LocationClient;
import com.jqyd.android.module.lbs.Interface.ILocationListener;
import com.jqyd.android.module.lbs.bean.LocationInfo;
import com.tencent.mm.sdk.platformtools.Util;
public class RouteNavActivity extends FragmentActivity implements OnMarkerClickListener, OnMapClickListener,
OnInfoWindowClickListener, InfoWindowAdapter,AMapNaviListener {
private Context context = this;
private final int zoomLevel = 12;// 放大倍数
/**
* 开始定位
*/
private final int ROUTE_START_SEARCH = 2000;
/**
* 定位结束
*/
private final int ROUTE_SEARCH_RESULT = 2002;
/**
* 搜索路线失败
*/
private final int ROUTE_SEARCH_ERROR = 2004;
/**
* 搜索到路线
*/
private final int ROUTE_SEARCH_DRIVE_RESULT = 2005;
private final String START_MARKER_TEXT = "我的位置";
private final String END_MARKER_TEXT = "目的地位置";
// 地图和导航核心逻辑类
private AMap mMap;
private AMapNavi mAmapNavi;
private AutoCompleteTextView startTextView, endTextView;
private Dialog pd;
private String startpointName, endpointName;
private LatLonPoint startPoint = null, endPoint;
private LinearLayout routeNav;
private ImageButton routePre, routeNext;
private boolean isClickStart, isClickTarget;
private Marker startMk, targetMk, defaultMarker;
private int mode = Route.DrivingDefault;
private OptsharepreInterface sharePre;
private RouteSearch routeSearch;
private View route_FrameLayout, route_message, zoomout_layout,
less_time_BT, no_highway_BT, less_distance_BT;
private Button btnOption;
private ListView route_message_listView;
private TextView end_position, start_position;
private SimpleAdapter listViewAdapter;
private DriveRouteResult result;
private ImageView navi_bt;
private Parcelable endp;
private DrivingRouteOverlay drivingRouteOverlay;
private boolean mIsGetGPS=false;//判断定位是否成功
// 记录导航种类,用于记录当前选择是驾车还是步行
private int mTravelMethod = DRIVER_NAVI_METHOD;
private static final int DRIVER_NAVI_METHOD = 0;// 驾车导航
private static final int WALK_NAVI_METHOD = 1;// 步行导航
// 计算路的状态
private final static int GPSNO = 0;// 使用我的位置进行计算、GPS定位还未成功状态
private final static int CALCULATEERROR = 1;// 启动路径计算失败状态
private final static int CALCULATESUCCESS = 2;// 启动路径计算成功状态
// 记录起点、终点位置
private NaviLatLng mStartPoint = new NaviLatLng();
private NaviLatLng mEndPoint = new NaviLatLng();
// 驾车路径规划起点,终点的list
private List<NaviLatLng> mStartPoints = new ArrayList<NaviLatLng>();
private List<NaviLatLng> mEndPoints = new ArrayList<NaviLatLng>();
private AMapNaviListener mAmapNaviListener;
private MapView mMapView;
private TextToSpeechUtil textToSpeechUtil;//语音播报工具类
private LocationManagerProxy locationManager;
private ImageView hide_bt;
private LinearLayout navi_layout;
private int imgWidth,imgHeight,winWidth,winHeight,imgTwowidth;
private boolean bl=true;//判断隐藏状态,true 表示显示 false 表示隐藏
/**
* 搜索结束数据
*/
private List<Map<String, Object>> data;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
doinit();
}
private void doinit() {
initParam();
initWidgets();
inItLisener();
initMapAndNavi();
initUI();
}
private void initParam() {
routeSearch = new RouteSearch(this);
sharePre = new OptsharepreInterface(this);
endp = getIntent().getParcelableExtra(Constants.PARAM_ENDPOINT);//91、31
if (null != endp && endp instanceof LatLonPoint) {
endPoint = (LatLonPoint) endp;
mEndPoint=new NaviLatLng(endPoint.getLatitude(),endPoint.getLongitude());
mEndPoints.add(mEndPoint);
}
endpointName = getIntent().getStringExtra(Constants.PARAM_ENDPOINT_NAME);
routeHandler.sendEmptyMessage(ROUTE_START_SEARCH);
}
private void initWidgets() {
setContentView(R.layout.route);
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (mMap != null) {
mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
}
}
mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
textToSpeechUtil=new TextToSpeechUtil(RouteNavActivity.this);
UiSettings mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);//这个方法设置了地图是否允许显示缩放按钮。
startTextView = (AutoCompleteTextView) findViewById(R.id.autotextview_roadsearch_start);
endTextView = (AutoCompleteTextView) findViewById(R.id.autotextview_roadsearch_goals);
route_FrameLayout = findViewById(R.id.route_FrameLayout);
route_message = findViewById(R.id.route_message);
btnOption = (Button) findViewById(R.id.btnOption);
routeNav = (LinearLayout) findViewById(R.id.LinearLayoutLayout_index_bottom);
routePre = (ImageButton) findViewById(R.id.pre_index);
routeNext = (ImageButton) findViewById(R.id.next_index);
start_position = (TextView) findViewById(R.id.start_position);
end_position = (TextView) findViewById(R.id.end_position);
route_message_listView = (ListView) findViewById(R.id.route_message_listView);
less_time_BT = findViewById(R.id.less_time_BT);
less_distance_BT = findViewById(R.id.less_distance_BT);
no_highway_BT = findViewById(R.id.no_highway_BT);
zoomout_layout = findViewById(R.id.zoomout_layout);
navi_bt=(ImageView) findViewById(R.id.navi_bt);
hide_bt=(ImageView) findViewById(R.id.hide_bt);
navi_layout=(LinearLayout) findViewById(R.id.navi_layout);
getPixels();
AMapNavi.getInstance(this).setAMapNaviListener(this);
}
/**
* @author lifei
* 根据屏幕大小设置导航按钮宽高
*/
@SuppressLint("NewApi")
private void getPixels(){
DisplayMetrics outMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
winWidth=outMetrics.widthPixels;
winHeight=outMetrics.heightPixels;
//图片的宽高比为2:1
imgWidth=(int) (winWidth*0.2);
imgHeight=(int) (imgWidth*0.5);
imgTwowidth=(int) (imgHeight*0.61);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(imgWidth,imgHeight);
params.setMargins(0, 0, -8, 0);
navi_bt.setLayoutParams(params);
LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(imgTwowidth,imgHeight);
params1.setMargins(-8, 0, 0, 0);
hide_bt.setLayoutParams(params1);
}
private void inItLisener() {
}
private void initUI() {
less_time_BT.setBackgroundColor(getResources().getColor(
R.color.themeColor));
if (null != startPoint) {
updateView(new LatLng(startPoint.getLatitude(),
startPoint.getLongitude()));
}
if (null != startpointName && TextUtils.isEmpty(endpointName)) {
endTextView.setText("地图上的点");
} else {
endTextView.setText(endpointName);
}
}
/**
* 初始化地图和导航相关内容
*/
private void initMapAndNavi() {
// // 初始语音播报资源
// setVolumeControlStream(AudioManager.STREAM_MUSIC);// 设置声音控制
// //语音播报开始
// mAmapNavi = AMapNavi.getInstance(this);// 初始化导航引擎
}
private void setUpBaiduLocation() {
/**
* 接收百度定位类传来的参数
*/
LocationClient client = new LocationClient(context);
client.request(new ILocationListener() {
@Override
public void onReceiveLocInfo(int locType, LocationInfo loc) {
startPoint = new LatLonPoint(loc.getLat(), loc.getLon());
if(startPoint!=null&&locType==1){
mIsGetGPS=true;
}
mStartPoint =new NaviLatLng(startPoint.getLatitude(), startPoint.getLongitude());
mStartPoints.add(mStartPoint);
sharePre.putPres(Constants.PREFERENCE_KEY_LATITUDE,
String.valueOf(startPoint.getLatitude()));
sharePre.putPres(Constants.PREFERENCE_KEY_LONGITUDE,
String.valueOf(startPoint.getLongitude()));
routeHandler.sendEmptyMessage(ROUTE_SEARCH_RESULT);
}
});
}
/**
* 高德定位
*/
// private void setGaoDeLoc(){
//
// locationManager = LocationManagerProxy
// .getInstance(this);
// // API定位采用混合定位方式,第一个参数是定位provider,第二个参数时间是指定为一次,第三个参数距离间隔单位是米,第四个参数是定位监听者
// locationManager.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 200, new AMapLocationListener() {
//
// @Override
// public void onStatusChanged(String provider, int status, Bundle extras) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onProviderEnabled(String provider) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onProviderDisabled(String provider) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onLocationChanged(Location location) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onLocationChanged(AMapLocation arg0) {
// // TODO Auto-generated method stub
// if(arg0!=null){
// startPoint = new LatLonPoint(arg0.getLatitude(), arg0.getLongitude());
// if(startPoint!=null){
// mIsGetGPS=true;
// }
// mStartPoint =new NaviLatLng(startPoint.getLatitude(), startPoint.getLongitude());
// mStartPoints.add(mStartPoint);
// sharePre.putPres(Constants.PREFERENCE_KEY_LATITUDE,
// String.valueOf(startPoint.getLatitude()));
// sharePre.putPres(Constants.PREFERENCE_KEY_LONGITUDE,
// String.valueOf(startPoint.getLongitude()));
// routeHandler.sendEmptyMessage(ROUTE_SEARCH_RESULT);
// }else{
// Toast.makeText(RouteNavActivity.this, "定位失败",0).show();
// }
// }
// });
//
//
// }
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBack:
finish();
break;
case R.id.btnOption:
if (btnOption.getText().equals("地图")) {
btnOption.setText("详情"); // 隐藏地图页面显示文字详情
route_FrameLayout.setVisibility(View.VISIBLE);
route_message.setVisibility(View.GONE);
routeNav.setVisibility(View.VISIBLE);
zoomout_layout.setVisibility(View.VISIBLE);
navi_layout.setVisibility(View.VISIBLE);
} else {
btnOption.setText("地图"); // 隐藏文字详情页面显示地图
route_FrameLayout.setVisibility(View.GONE);
route_message.setVisibility(View.VISIBLE);
routeNav.setVisibility(View.GONE);
zoomout_layout.setVisibility(View.GONE);
navi_layout.setVisibility(View.GONE);
}
break;
case R.id.less_time_BT:
mode = RouteSearch.DrivingMultiStrategy;
less_time_BT.setBackgroundColor(getResources().getColor(
R.color.themeColor));
less_distance_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
no_highway_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
startSearchResult();
break;
case R.id.less_distance_BT:
mode = RouteSearch.DrivingShortDistance;
less_time_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
less_distance_BT.setBackgroundColor(getResources().getColor(
R.color.themeColor));
no_highway_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
startSearchResult();
break;
case R.id.no_highway_BT:
mode = RouteSearch.DrivingNoHighWaySaveMoney;
less_time_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
less_distance_BT.setBackgroundDrawable(getResources().getDrawable(
R.drawable.button_btm));
no_highway_BT.setBackgroundColor(getResources().getColor(
R.color.themeColor));
startSearchResult();
break;
case R.id.pre_index:
if (result != null) {
boolean enablePre = drivingRouteOverlay.showPrePopInfo();
if (!enablePre) {
routePre.setBackgroundResource(R.drawable.prev_disable);
routeNext.setBackgroundResource(R.drawable.btn_route_next);
} else {
routePre.setBackgroundResource(R.drawable.btn_route_pre);
routeNext.setBackgroundResource(R.drawable.btn_route_next);
}
}
break;
case R.id.next_index:
if (result != null) {
boolean enableNext = drivingRouteOverlay.showNextPopInfo();
if (!enableNext) {
routePre.setBackgroundResource(R.drawable.btn_route_pre);
routeNext.setBackgroundResource(R.drawable.next_disable);
} else {
routePre.setBackgroundResource(R.drawable.btn_route_pre);
routeNext.setBackgroundResource(R.drawable.btn_route_next);
}
}
break;
case R.id.zoomin_bt:
mMap.animateCamera(CameraUpdateFactory.zoomIn(), 1000, null);
break;
case R.id.zoomout_bt:
mMap.animateCamera(CameraUpdateFactory.zoomOut(), 1000, null);
break;
case R.id.navi_bt://导航按钮
calculateRoute();
break;
case R.id.hide_bt://隐藏显示按钮
getAnimation();
break;
default:
break;
}
}
/**
* @author lifei
* 设置显示和隐藏的动画
*/
@SuppressLint("NewApi")
private void getAnimation(){
if(bl){
bl=false;
navi_layout.animate().translationX(-imgWidth).setDuration(1000).withEndAction(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
hide_bt.setBackground(getResources().getDrawable(R.drawable.show_bg));
}
});//隐藏
}else{
bl=true;
navi_layout.animate().translationX(0).setDuration(1000).withEndAction(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
hide_bt.setBackground(getResources().getDrawable(R.drawable.hide_bg));
}
});//显示
}
}
/**
* 算路的方法,根据选择可以进行行车和步行两种方式进行路径规划
*/
private void calculateRoute() {
if(!mIsGetGPS){
setUpBaiduLocation();
myshowDialog(1);
return;
}
mIsGetGPS=false;
switch (mTravelMethod) {
// 驾车导航,默认为驾车导航
case DRIVER_NAVI_METHOD:
int driverIndex = calculateDriverRoute();
if (driverIndex == CALCULATEERROR) {
Toast.makeText(RouteNavActivity.this, "路线计算失败,检查参数情况", 0).show();
return;
} else if (driverIndex == GPSNO) {
return;
}
break;
// 步行导航
case WALK_NAVI_METHOD:
break;
}
}
/**
* 对行车路线进行规划
*/
private int calculateDriverRoute() {
int driveMode = getDriveMode();
int code = CALCULATEERROR;
if (AMapNavi.getInstance(this).calculateDriveRoute(mStartPoints, mEndPoints,
null, driveMode)) {
code = CALCULATESUCCESS;
} else {
code = CALCULATEERROR;
}
return code;
}
/**
* 根据选择,获取行车策略
* @return
*/
private int getDriveMode() {
// 不走高速
if(mode==RouteSearch.DrivingNoHighWaySaveMoney){
return AMapNavi.DrivingNoExpressways;
}
// 距离最短
else if(mode == RouteSearch.DrivingShortDistance){
return AMapNavi.DrivingShortDistance;
}
// 时间最短 mode == RouteSearch.DrivingMultiStrategy
else{
return AMapNavi.DrivingDefault;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onInfoWindowClick(Marker arg0) {
isClickStart = false;
isClickTarget = false;
if (startMk.equals(arg0)) {
startTextView.setText(START_MARKER_TEXT);
startPoint = AMapUtil.convertToLatLonPoint(startMk.getPosition());
startMk.setTitle("起点");
View view = getInfoWindow(startMk);
view.setBackgroundColor(getResources().getColor(R.color.gray));
} else if (targetMk.equals(arg0)) {
endTextView.setText(END_MARKER_TEXT);
endPoint = AMapUtil.convertToLatLonPoint(targetMk.getPosition());
targetMk.setTitle("终点");
View view = getInfoWindow(targetMk);
view.setBackgroundColor(getResources().getColor(R.color.gray));
}
releaseListener();
}
@Override
public boolean onMarkerClick(Marker arg0) {
return false;
}
@Override
public void onMapClick(LatLng latng) {
mMap.clear();
if (isClickStart) {
startMk = mMap.addMarker(new MarkerOptions()
.anchor(0.5f, 1)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.point)).position(latng)
.title("点击选择为起点"));
startMk.showInfoWindow();
} else if (isClickTarget) {
targetMk = mMap.addMarker(new MarkerOptions()
.anchor(0.5f, 1)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.point)).position(latng)
.title("点击选择为目的地"));
targetMk.showInfoWindow();
}
}
@Override
public View getInfoContents(Marker arg0) {
return null;
}
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
/**
* 销毁监听
*/
private void releaseListener() {
mMap.setOnMapClickListener(null);
mMap.setOnMarkerClickListener(null);
mMap.setOnInfoWindowClickListener(null);
mMap.setInfoWindowAdapter(null);
}
/**
* 切换视图
* @param poi
*/
private void updateView(LatLng poi) {
CameraPosition camerapoint = new CameraPosition.Builder().target(poi)
.zoom(zoomLevel).bearing(0).tilt(45).build();
changeCamera(CameraUpdateFactory.newCameraPosition(camerapoint), null);
}
/**
* 根据动画按钮状态,调用函数animateCamera或moveCamera来改变可视区
*/
private void changeCamera(CameraUpdate update, CancelableCallback callback) {
mMap.moveCamera(update);
}
private Dialog myshowDialog(int i) {
if (i == 1) {
if (RouteNavActivity.this != null && !RouteNavActivity.this.isFinishing()) {
pd = ProgressDialogStyle.createLoadingDialog(
RouteNavActivity.this, "正在定位...");
pd.show();
}
} else if (i == 2) {
if (RouteNavActivity.this != null && !RouteNavActivity.this.isFinishing()) {
pd = ProgressDialogStyle.createLoadingDialog(
RouteNavActivity.this, "正在获取路线...");
pd.show();
}
}
return pd;
}
private void myremoveDialog(Dialog pd) {
if (pd != null) {
pd.dismiss();
}
}
/**
* 查询路径规划起点
*/
private void startSearchResult() {
if (startPoint != null) {
endSearchResult();
}
}
/**
* 查询路径规划终点
*/
private void endSearchResult() {
if (endPoint != null) {
searchRouteResult(startPoint, endPoint);
}
}
/**
* 构造路径规划的起点和终点坐标,构造完毕,通知UI线程搜索路线
* @param startPoint
* @param endPoint
*/
private void searchRouteResult(LatLonPoint startPoint, LatLonPoint endPoint) {
pd = myshowDialog(2);
com.amap.api.services.core.LatLonPoint startPoint1 = new com.amap.api.services.core.LatLonPoint(
startPoint.getLatitude(), startPoint.getLongitude());
com.amap.api.services.core.LatLonPoint endPoint1 = new com.amap.api.services.core.LatLonPoint(
endPoint.getLatitude(), endPoint.getLongitude());
//构造路径规划的起点和终点坐标。
final RouteSearch.FromAndTo fromAndTo1 = new RouteSearch.FromAndTo(
startPoint1, endPoint1);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
DriveRouteQuery query = new DriveRouteQuery(fromAndTo1, mode,
null, null, "");
try {
result = routeSearch.calculateDriveRoute(query);//驾车路径规划的结果集
routeHandler.sendEmptyMessage(ROUTE_SEARCH_DRIVE_RESULT);
} catch (com.amap.api.services.core.AMapException e) {
Message msg = new Message();
msg.what = ROUTE_SEARCH_ERROR;
msg.obj = e.getErrorMessage();
routeHandler.sendMessage(msg);
e.printStackTrace();
}
}
});
t.start();
}
@SuppressLint("HandlerLeak")
private Handler routeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//以下按@数字表示执行先后顺序
case ROUTE_SEARCH_DRIVE_RESULT://@3,构造规划完毕后,搜索路线成功
if (null != result&& result.getPaths().get(0).getSteps().size() > 0) {//result 开始对结束对31、91
start_position.setText("我的位置");
end_position.setText("终点");
onDriveRouteSearched(result, 0);
}
myremoveDialog(pd);
break;
case ROUTE_SEARCH_ERROR://@3,构造规划完毕后,搜索路线失败
myremoveDialog(pd);
UIUtil.showMsg(RouteNavActivity.this, "导航失败,请确认您的网络后重新请求");
break;
case ROUTE_START_SEARCH://@1,先执行定位,用户当前位置
pd = myshowDialog(1);
setUpBaiduLocation();
// setGaoDeLoc();
break;
case ROUTE_SEARCH_RESULT://@2,定位获取到数据后,构造路径规划
myremoveDialog(pd);
if (RouteNavActivity.this != null && !RouteNavActivity.this.isFinishing()) {
startSearchResult();
drawMarker();
updateView(new LatLng(startPoint.getLatitude(),startPoint.getLongitude()));
}
break;
default:
break;
}
}
};
private int getDrawable(String message) {
int drawable;
if (message.contains("左转")) {
drawable = R.drawable.turn_left;
} else if (message.contains("右转")) {
drawable = R.drawable.turn_right;
} else if (message.contains("右前方")) {
drawable = R.drawable.turn_right2;
} else if (message.contains("左前方")) {
drawable = R.drawable.turn_left1;
} else if (message.contains("掉头")) {
drawable = R.drawable.turn_back;
} else if (message.contains("直行")) {
drawable = R.drawable.turn_front;
} else {
drawable = R.drawable.turn_front;
}
return drawable;
}
private void drawMarker() {
// 对地图添加一个marker
defaultMarker = mMap.addMarker(new MarkerOptions()
.position(
new LatLng(startPoint.getLatitude(), startPoint
.getLongitude())).title("您的位置").snippet("")
.icon(BitmapDescriptorFactory.defaultMarker())
.draggable(false));
defaultMarker.showInfoWindow();
}
/**
* 根据搜索的路线数据在地图上画出导航路线
* @param result 驾车路径规划的结果集
* @param rCode
*/
private void onDriveRouteSearched(DriveRouteResult result, int rCode) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
DriveRouteResult driveRouteResult = result;
DrivePath drivePath = driveRouteResult.getPaths().get(0);//返回驾车路径规划方案
mMap.clear();// 清理地图上的所有覆盖物
drivingRouteOverlay = new DrivingRouteOverlay(this, mMap,
result);
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addMarkerLine();
routeNav.setVisibility(View.VISIBLE);
routePre.setBackgroundResource(R.drawable.prev_disable);
routeNext.setBackgroundResource(R.drawable.btn_route_next);
data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < drivePath.getSteps().size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("list_item", drivePath.getSteps().get(i)
.getInstruction());
int draw = getDrawable(drivePath.getSteps().get(i)
.getAction());
map.put("draw", draw);
data.add(map);
}
String[] from = { "draw", "list_item" };
int[] to = { R.id.ivFangxiang, R.id.tvTitle1 };
listViewAdapter = new SimpleAdapter(
RouteNavActivity.this, data,
R.layout.listview_item_route, from, to);
route_message_listView.setAdapter(listViewAdapter);
if (route_FrameLayout.getVisibility() == View.GONE) {
routeNav.setVisibility(View.GONE);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
AMapNavi.getInstance(this).removeAMapNaviListener(this);
if (pd != null) {
pd.dismiss();
}
}
//AMapNaviListener监听事件
@Override
public void onArriveDestination() {
// TODO Auto-generated method stub
}
@Override
public void onArrivedWayPoint(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCalculateRouteFailure(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCalculateRouteSuccess() {
// TODO Auto-generated method stub
Intent intent = new Intent(RouteNavActivity.this,
SimpleNaviActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Bundle bundle=new Bundle();
bundle.putInt(Constants.ACTIVITYINDEX, Constants.SIMPLEGPSNAVI);
bundle.putBoolean(Constants.ISEMULATOR, false);
bundle.putParcelable(Constants.PARAM_ENDPOINT, endPoint);//记录终点坐标,以便关闭导航时能取到该值
intent.putExtras(bundle);
startActivity(intent);
finish();
}
@Override
public void onEndEmulatorNavi() {
// TODO Auto-generated method stub
}
@Override
public void onGetNavigationText(int arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGpsOpenStatus(boolean arg0) {
// TODO Auto-generated method stub
}
@Override
public void onInitNaviFailure() {
// TODO Auto-generated method stub
}
@Override
public void onInitNaviSuccess() {
// TODO Auto-generated method stub
}
@Override
public void onLocationChange(AMapNaviLocation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onNaviInfoUpdate(NaviInfo arg0) {
// TODO Auto-generated method stub
}
@Override
@Deprecated
public void onNaviInfoUpdated(AMapNaviInfo arg0) {
// TODO Auto-generated method stub
}
@Override
public void onReCalculateRouteForTrafficJam() {
// TODO Auto-generated method stub
}
@Override
public void onReCalculateRouteForYaw() {
// TODO Auto-generated method stub
}
@Override
public void onStartNavi(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTrafficStatusUpdate() {
// TODO Auto-generated method stub
}
}