package com.withiter.quhao.activity;
import android.app.ProgressDialog;
import android.location.Location;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
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.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.overlay.BusRouteOverlay;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.maps.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
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.BusRouteQuery;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.withiter.quhao.R;
import com.withiter.quhao.data.MerchantData;
/**
* AMapV2地图中简单介绍route搜索
*/
public class MerchantRouteActivity extends QuhaoBaseActivity implements OnMarkerClickListener,
OnInfoWindowClickListener, InfoWindowAdapter,
OnRouteSearchListener, OnClickListener, AMapLocationListener {
private AMap aMap;
private MapView mapView;
private Button drivingButton;
private Button busButton;
private Button walkButton;
private TextView routeSearchView;
private ProgressDialog progDialog = null;// 搜索时进度条
private int busMode = RouteSearch.BusDefault;// 公交默认模式
private int drivingMode = RouteSearch.DrivingDefault;// 驾车默认模式
private int walkMode = RouteSearch.WalkDefault;// 步行默认模式
private BusRouteResult busRouteResult;// 公交模式查询结果
private DriveRouteResult driveRouteResult;// 驾车模式查询结果
private WalkRouteResult walkRouteResult;// 步行模式查询结果
private int routeType = 1;// 1代表公交模式,2代表驾车模式,3代表步行模式
private LatLonPoint startPoint = null;
private LatLonPoint endPoint = null;
private RouteSearch routeSearch;
public ArrayAdapter<String> aAdapter;
private LocationManagerProxy mAMapLocationManager;
private MerchantData merchant;
private AMapLocation firstLocation = null;
private boolean isFirstLocation = false;
@Override
protected void onCreate(Bundle bundle) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.route_activity);
super.onCreate(bundle);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(bundle);// 此方法必须重写
merchant = getIntent().getParcelableExtra("merchant");
startPoint = new LatLonPoint(merchant.getLat(), merchant.getLng());
routeSearchView = (TextView) this.findViewById(R.id.route_search);
routeSearchView.setText("我的位置-->"+ merchant.getName());
btnBack.setOnClickListener(goBack(this));
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
registerListener();
}
routeSearch = new RouteSearch(this);
routeSearch.setRouteSearchListener(this);
busButton = (Button) findViewById(R.id.imagebtn_roadsearch_tab_transit);
busButton.setOnClickListener(this);
drivingButton = (Button) findViewById(R.id.imagebtn_roadsearch_tab_driving);
drivingButton.setOnClickListener(this);
walkButton = (Button) findViewById(R.id.imagebtn_roadsearch_tab_walk);
walkButton.setOnClickListener(this);
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
Thread requestLocation = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
try {
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy
.getInstance(MerchantRouteActivity.this);
/*
* mAMapLocManager.setGpsEnable(false);//
* 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true
*/
// Location SDK定位采用GPS和网络混合定位方式,时间最短是5000毫秒,否则无效
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 10000, 100,
MerchantRouteActivity.this);
// locationHandler.postDelayed(locationRunnable, 60000);// 设置超过12秒还没有定位到就停止定位
} else {
/*
* mAMapLocManager.setGpsEnable(false);//
* 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true
*/
// Location SDK定位采用GPS和网络混合定位方式,时间最短是5000毫秒,否则无效
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 10000, 100,
MerchantRouteActivity.this);
// locationHandler.postDelayed(locationRunnable, 60000);// 设置超过12秒还没有定位到就停止定位
}
} catch (Exception e) {
}
finally
{
Looper.loop();
}
}
});
requestLocation.start();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(this);
mAMapLocationManager.destory();
}
mAMapLocationManager = null;
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* 选择公交模式
*/
private void busRoute() {
routeType = 1;// 标识为公交模式
busMode = RouteSearch.BusDefault;
drivingButton.setBackgroundResource(R.drawable.mode_driving_off);
busButton.setBackgroundResource(R.drawable.mode_transit_on);
walkButton.setBackgroundResource(R.drawable.mode_walk_off);
}
/**
* 选择驾车模式
*/
private void drivingRoute() {
routeType = 2;// 标识为驾车模式
drivingMode = RouteSearch.DrivingSaveMoney;
drivingButton.setBackgroundResource(R.drawable.mode_driving_on);
busButton.setBackgroundResource(R.drawable.mode_transit_off);
walkButton.setBackgroundResource(R.drawable.mode_walk_off);
}
/**
* 选择步行模式
*/
private void walkRoute() {
routeType = 3;// 标识为步行模式
walkMode = RouteSearch.WalkMultipath;
drivingButton.setBackgroundResource(R.drawable.mode_driving_off);
busButton.setBackgroundResource(R.drawable.mode_transit_off);
walkButton.setBackgroundResource(R.drawable.mode_walk_on);
}
@Override
public void onInfoWindowClick(Marker marker) {
}
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return false;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
/**
* 注册监听
*/
private void registerListener() {
aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
aMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器
aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
}
/**
* 显示进度框
*/
private void showProgressDialog() {
if (progDialog == null)
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setIndeterminate(false);
progDialog.setCancelable(true);
progDialog.setMessage("正在搜索");
progDialog.show();
}
/**
* 隐藏进度框
*/
private void dissmissProgressDialog() {
if (progDialog != null) {
progDialog.dismiss();
}
}
/**
* 开始搜索路径规划方案
*/
public void searchRouteResult(LatLonPoint startPoint, LatLonPoint endPoint) {
showProgressDialog();
final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
startPoint, endPoint);
if (routeType == 1) {// 公交路径规划
BusRouteQuery query = new BusRouteQuery(fromAndTo, busMode, firstLocation.getCity(), 0);// 第一个参数表示路径规划的起点和终点,第二个参数表示公交查询模式,第三个参数表示公交查询城市区号,第四个参数表示是否计算夜班车,0表示不计算
routeSearch.calculateBusRouteAsyn(query);// 异步路径规划公交模式查询
} else if (routeType == 2) {// 驾车路径规划
DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode,
null, null, "");// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
} else if (routeType == 3) {// 步行路径规划
WalkRouteQuery query = new WalkRouteQuery(fromAndTo, walkMode);
routeSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询
}
}
/**
* 公交路线查询回调
*/
@Override
public void onBusRouteSearched(BusRouteResult result, int rCode) {
dissmissProgressDialog();
if (rCode == 0) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
busRouteResult = result;
BusPath busPath = busRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
BusRouteOverlay routeOverlay = new BusRouteOverlay(this, aMap,
busPath, busRouteResult.getStartPos(),
busRouteResult.getTargetPos());
routeOverlay.removeFromMap();
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
} else {
Toast.makeText(this, "对不起,没有搜索到相关数据!", Toast.LENGTH_SHORT).show();
}
} else if (rCode == 27) {
Toast.makeText(this, "搜索失败,请检查网络连接!", Toast.LENGTH_SHORT).show();
} else if (rCode == 32) {
Toast.makeText(this, "key验证无效!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "未知错误,请稍后重试!", Toast.LENGTH_SHORT).show();
}
}
/**
* 驾车结果回调
*/
@Override
public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
dissmissProgressDialog();
if (rCode == 0) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
driveRouteResult = result;
DrivePath drivePath = driveRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
this, aMap, drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else {
Toast.makeText(this, "对不起,没有搜索到相关数据!", Toast.LENGTH_SHORT).show();
}
} else if (rCode == 27) {
Toast.makeText(this, "搜索失败,请检查网络连接!", Toast.LENGTH_SHORT).show();
} else if (rCode == 32) {
Toast.makeText(this, "key验证无效!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "未知错误,请稍后重试!", Toast.LENGTH_SHORT).show();
}
}
/**
* 步行路线结果回调
*/
@Override
public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
dissmissProgressDialog();
if (rCode == 0) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
walkRouteResult = result;
WalkPath walkPath = walkRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this,
aMap, walkPath, walkRouteResult.getStartPos(),
walkRouteResult.getTargetPos());
walkRouteOverlay.removeFromMap();
walkRouteOverlay.addToMap();
walkRouteOverlay.zoomToSpan();
} else {
Toast.makeText(this, "对不起,没有搜索到相关数据!", Toast.LENGTH_SHORT).show();
}
} else if (rCode == 27) {
Toast.makeText(this, "搜索失败,请检查网络连接!", Toast.LENGTH_SHORT).show();
} else if (rCode == 32) {
Toast.makeText(this, "key验证无效!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "未知错误,请稍后重试!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
if (null == endPoint) {
Toast.makeText(this, "亲,稍等一下,正在定位哦!", Toast.LENGTH_SHORT).show();
return;
}
switch (v.getId()) {
case R.id.imagebtn_roadsearch_tab_transit:
busRoute();
searchRouteResult(startPoint, endPoint);// 进行路径规划搜索
break;
case R.id.imagebtn_roadsearch_tab_driving:
drivingRoute();
searchRouteResult(startPoint, endPoint);// 进行路径规划搜索
break;
case R.id.imagebtn_roadsearch_tab_walk:
walkRoute();
searchRouteResult(startPoint, endPoint);// 进行路径规划搜索
break;
default:
break;
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(AMapLocation location) {
Log.e("wjzwjz", "NearByFragment onLocationChanged");
if (null != location) {
if (!isFirstLocation) {
isFirstLocation = true;
firstLocation = location;
float bearing = aMap.getCameraPosition().bearing;
aMap.setMyLocationRotateAngle(bearing);// 设置小蓝点旋转角度
endPoint = new LatLonPoint(firstLocation.getLatitude(), firstLocation.getLongitude());
searchRouteResult(startPoint, endPoint);
} else {
float distance = firstLocation.distanceTo(location);
if (distance > 100) {
firstLocation = location;
float bearing = aMap.getCameraPosition().bearing;
aMap.setMyLocationRotateAngle(bearing);// 设置小蓝点旋转角度
endPoint = new LatLonPoint(firstLocation.getLatitude(), firstLocation.getLongitude());
searchRouteResult(startPoint, endPoint);
} else {
return;
}
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}