android 步行距离,Android 高德地图 步行 路线规划

路线规划就是给RouteSearch设置一个监听,里面有步行规划,骑车,驾车,公交等线路的规划

第 1 步,初始化 RouteSearch 对象

routeSearch = new RouteSearch(this);

第 2 步,设置数据回调监听器

routeSearch.setRouteSearchListener(this);

第 3 步,设置搜索参数

通过 WalkRouteQuery(RouteSearch.FromAndTo fromAndTo, int mode) 设置搜索条件。其中:

fromAndTo,路径的起终点;

mode,计算路径的模式。SDK提供两种模式:RouteSearch.WALK_DEFAULT 和 RouteSearch.WALK_MULTI_PATH。

//初始化query对象,fromAndTo是包含起终点信息,walkMode是步行路径规划的模式

WalkRouteQuery query = new WalkRouteQuery(fromAndTo, walkMode);

第 4 步,发送请求

使用类 RouteSearch 的 calculateWalkRouteAsyn(WalkRouteQuery query) 方法进行步行规划路径计算。

routeSearch.calculateWalkRouteAsyn(query);//开始算路

第 5 步,接收数据

在 RouteSearch.OnRouteSearchListener 接口回调方法 void onWalkRouteSearched(WalkRouteResult walkRouteResult,int rCode) 处理步行规划路径结果。返回的信息中您可以获得路段的距离、步行的预计时间、步行路段的坐标点、步行路段的道路名称、导航主要操作等信息。显示效果如下:

说明:

1)可以在回调中解析result,获取骑行的路径。

2)result.getPaths()可以获取到 WalkPath 列表,步行路径的详细信息可参考 WalkPath 类。

3)返回结果成功或者失败的响应码。1000为成功,其他为失败(详细信息参见网站开发指南-实用工具-错误码对照表)

下面一个步行规划的图:

主要的方法MainActivity

public class MainActivity extends Activity implements AMap.OnMapClickListener,

AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter, RouteSearch.OnRouteSearchListener {

private AMap aMap;

private MapView mapView;

private Context mContext;

private RouteSearch mRouteSearch;

private WalkRouteResult mWalkRouteResult;

private LatLonPoint mStartPoint = new LatLonPoint(39.942295, 116.335891);//起点,116.335891,39.942295

private LatLonPoint mEndPoint = new LatLonPoint(39.995576, 116.481288);//终点,116.481288,39.995576

private final int ROUTE_TYPE_WALK = 3;

private RelativeLayout mBottomLayout, mHeadLayout;

private TextView mRotueTimeDes, mRouteDetailDes;

private ProgressDialog progDialog = null;// 搜索时进度条

@Override

protected void onCreate(Bundle bundle) {

super.onCreate(bundle);

setContentView(R.layout.activity_main);

mContext = this.getApplicationContext();

mapView = (MapView) findViewById(R.id.route_map);

mapView.onCreate(bundle);// 此方法必须重写

init();

setfromandtoMarker();

searchRouteResult(ROUTE_TYPE_WALK, RouteSearch.WalkDefault);

}

private void setfromandtoMarker() {

aMap.addMarker(new MarkerOptions()

.position(AMapUtil.convertToLatLng(mStartPoint))

.icon(BitmapDescriptorFactory.fromResource(R.drawable.start)));

aMap.addMarker(new MarkerOptions()

.position(AMapUtil.convertToLatLng(mEndPoint))

.icon(BitmapDescriptorFactory.fromResource(R.drawable.end)));

}

/**

* 初始化AMap对象

*/

private void init() {

if (aMap == null) {

aMap = mapView.getMap();

}

registerListener();

mRouteSearch = new RouteSearch(this);

mRouteSearch.setRouteSearchListener(this);

mBottomLayout = (RelativeLayout) findViewById(R.id.bottom_layout);

mHeadLayout = (RelativeLayout) findViewById(R.id.routemap_header);

mHeadLayout.setVisibility(View.GONE);

mRotueTimeDes = (TextView) findViewById(R.id.firstline);

mRouteDetailDes = (TextView) findViewById(R.id.secondline);

}

/**

* 注册监听

*/

private void registerListener() {

aMap.setOnMapClickListener(this);

aMap.setOnMarkerClickListener(this);

aMap.setOnInfoWindowClickListener(this);

aMap.setInfoWindowAdapter(this);

}

@Override

public View getInfoContents(Marker arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

public View getInfoWindow(Marker arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onInfoWindowClick(Marker arg0) {

// TODO Auto-generated method stub

}

@Override

public boolean onMarkerClick(Marker arg0) {

// TODO Auto-generated method stub

return false;

}

@Override

public void onMapClick(LatLng arg0) {

// TODO Auto-generated method stub

}

/**

* 开始搜索路径规划方案

*/

public void searchRouteResult(int routeType, int mode) {

if (mStartPoint == null) {

ToastUtil.show(mContext, "定位中,稍后再试...");

return;

}

if (mEndPoint == null) {

ToastUtil.show(mContext, "终点未设置");

}

showProgressDialog();

final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(

mStartPoint, mEndPoint);

if (routeType == ROUTE_TYPE_WALK) {// 步行路径规划

RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, mode);

mRouteSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询

}

}

@Override

public void onBusRouteSearched(BusRouteResult result, int errorCode) {

//公交的路线规划

}

@Override

public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {

//自驾的路线

}

@Override

public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {

//步行路线规划

dissmissProgressDialog();

aMap.clear();// 清理地图上的所有覆盖物

if (errorCode == AMapException.CODE_AMAP_SUCCESS) {

if (result != null && result.getPaths() != null) {

if (result.getPaths().size() > 0) {

mWalkRouteResult = result;

final WalkPath walkPath = mWalkRouteResult.getPaths()

.get(0);

WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(

this, aMap, walkPath,

mWalkRouteResult.getStartPos(),

mWalkRouteResult.getTargetPos());

walkRouteOverlay.removeFromMap();

walkRouteOverlay.addToMap();

walkRouteOverlay.zoomToSpan();

mBottomLayout.setVisibility(View.VISIBLE);

int dis = (int) walkPath.getDistance();

int dur = (int) walkPath.getDuration();

String des = AMapUtil.getFriendlyTime(dur) + "(" + AMapUtil.getFriendlyLength(dis) + ")";

mRotueTimeDes.setText(des);

mRouteDetailDes.setVisibility(View.GONE);

mBottomLayout.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//显示详细的路径,怎么走 走多少米往哪个方向走等

Intent intent = new Intent(mContext,

WalkRouteDetailActivity.class);

intent.putExtra("walk_path", walkPath);

intent.putExtra("walk_result",

mWalkRouteResult);

startActivity(intent);

}

});

} else if (result != null && result.getPaths() == null) {

ToastUtil.show(mContext, R.string.no_result);

}

} else {

ToastUtil.show(mContext, R.string.no_result);

}

} else {

ToastUtil.showerror(this.getApplicationContext(), errorCode);

}

}

/**

* 显示进度框

*/

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();

}

}

/**

* 方法必须重写

*/

@Override

protected void onResume() {

super.onResume();

mapView.onResume();

}

/**

* 方法必须重写

*/

@Override

protected void onPause() {

super.onPause();

mapView.onPause();

}

/**

* 方法必须重写

*/

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

mapView.onSaveInstanceState(outState);

}

/**

* 方法必须重写

*/

@Override

protected void onDestroy() {

super.onDestroy();

mapView.onDestroy();

}

@Override

public void onRideRouteSearched(RideRouteResult arg0, int arg1) {

// TODO Auto-generated method stub

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值