驾车出行路线规划
驾车路径规划可以根据起终点和驾车路线的数据,使用 DrivingRouteOverlay 画出驾车路线图层,包括起终点和转弯点。另外也可以自定义起终点和驾车转弯点的图标。
第1步,初始化RouteSearch对象
routeSearch = new RouteSearch(this);
第2步,设置数据回调监听器
routeSearch.setRouteSearchListener(this);
第3步,设置搜索参数
通过 DriveRouteQuery(RouteSearch.FromAndTo fromAndTo, int mode, List<LatLonPoint> passedByPoints, List<List<LatLonPoint>> avoidpolygons, String avoidRoad) 设置搜索条件,方法对应的参数说明如下:
fromAndTo,路径的起点终点;mode,路径规划的策略,可选,默认为0-速度优先;详细策略请见驾车策略说明;passedByPoints,途经点,可选;avoidpolygons,避让区域,可选,支持32个避让区域,每个区域最多可有16个顶点。如果是四边形则有4个坐标点,如果是五边形则有5个坐标点。avoidRoad,避让道路,只支持一条避让道路,避让区域和避让道路同时设置,只有避让道路生效。
// fromAndTo包含路径规划的起点和终点,drivingMode表示驾车模式// 第三个参数表示途经点(最多支持16个),第四个参数表示避让区域(最多支持32个),第五个参数表示避让道路DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode, null, null, "");
第4步,发送请求
使用类 RouteSearch 的 calculateRideRouteAsyn(RideRouteQuery query) 方法进行骑行规划路径计算。
routeSearch.calculateDriveRouteAsyn(query);
第5步,接收数据
在 RouteSearch.OnRouteSearchListener 接口回调方法 void onDriveRouteSearched(DriveRouteResult result, int rCode) 处理驾车规划路径结果。返回的信息中包括:路线的距离、高速费用(仅针对7座以下轿车)、路况情况等等。
说明:
1)可以在回调中解析 result,获取驾车的路径。
2)result.getPaths()可以获取到 DrivePath 列表,驾车路径的详细信息可参考 DrivePath 类。
3)返回结果成功或者失败的响应码。1000为成功,其他为失败(详细信息参见网站开发指南-实用工具-错误码对照表)
public void onDriveRouteSearched(DriveRouteResult result, int rCode) { //解析result获取算路结果,可参考官方demo}
范例
package com.ldw.hello;import android.graphics.BitmapFactory;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.util.Log;import android.view.View;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.TextView;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps.AMap;import com.amap.api.maps.AMapOptions;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.MapView;import com.amap.api.maps.model.BitmapDescriptorFactory;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.maps.overlay.DrivingRouteOverlay;import com.amap.api.services.core.LatLonPoint;import com.amap.api.services.core.PoiItem;import com.amap.api.services.help.Inputtips;import com.amap.api.services.help.InputtipsQuery;import com.amap.api.services.help.Tip;import com.amap.api.services.poisearch.PoiResult;import com.amap.api.services.poisearch.PoiSearch;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.RideRouteResult;import com.amap.api.services.route.RouteSearch;import com.amap.api.services.route.WalkRouteResult;import java.util.ArrayList;import java.util.List;public class PassengerActivity extends AppCompatActivity {private MapView _mapView = null;private AMap _amap = null;private AMapLocationClient _amapLocationClient = null;private AMapLocationClientOption _amapLocationOption = null;private Button _bt_startOrder = null;private TextView _tv_srcAddr = null;private AutoCompleteTextView _attv_dstAddr = null;private Marker _selfMarker = null;private boolean isAddSelfMarker = false;private String _city = null;//当前定位所在的城市private LatLonPoint _startPoint = null;private LatLonPoint _endPoint = null;protected void initUI() {_mapView = (MapView)findViewById(R.id.PassengerMap);_bt_startOrder = (Button)findViewById(R.id.bt_startOrder);_tv_srcAddr = (TextView)findViewById(R.id.tv_passenger_srcAddr);_attv_dstAddr = (AutoCompleteTextView)findViewById(R.id.attv_passenger_dstAddr);}//以某个经纬度为中心来展示地图protected void moveMap(double latitude, double longtiude){LatLng lagLng = new LatLng(latitude, longtiude);//移动amap地图 以之前的缩放比例展示_amap.animateCamera(CameraUpdateFactory.newLatLngZoom(lagLng, _amap.getCameraPosition().zoom));}//向固定的经纬度添加一个标记protected void addMarkerToMap(double latitude, double longitude){_selfMarker = _amap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker))));}protected void createMap(Bundle savedInstanceState) {//展示地图容器_mapView.onCreate(savedInstanceState);//得到amap对象_amap = _mapView.getMap();//默认显示实时交通信息_amap.setTrafficEnabled(true);}//启动定位服务器protected void doLocation(){//1 创建一个客户端定位句柄_amapLocationClient = new AMapLocationClient(getApplicationContext());//1.5 给定位客户端设置一些属性_amapLocationOption = new AMapLocationClientOption();//每个5s定位一次_amapLocationOption.setInterval(3000);//_amapLocationOption.setOnceLocation(true);//将option设置给client对象_amapLocationClient.setLocationOption(_amapLocationOption);//2 给客户端句柄设置一个listenner来处理服务器返回的定位数据_amapLocationClient.setLocationListener(new AMapLocationListener() {@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {//onLocationChanged 就是如果服务器给客户端返回数据,调用的回调函数//aMapLocation 就是服务器给客户端返回的定位数据if (aMapLocation != null) {//服务器是有响应的if(aMapLocation.getErrorCode() == 0) {//定位成功,aMapLocation获取数据Log.e("Amap", "location succ address = "+ aMapLocation.getAddress());Log.e("Amap", "city = "+ aMapLocation.getCity());Log.e("Amap", "longtitude = " + aMapLocation.getLongitude());Log.e("Amap", "latitude = " + aMapLocation.getLatitude());if (isAddSelfMarker == false) {//在此位置添加一个标记addMarkerToMap(aMapLocation.getLatitude(), aMapLocation.getLongitude());isAddSelfMarker = true;//以自我为中心展示地图moveMap(aMapLocation.getLatitude(), aMapLocation.getLongitude());}if (_startPoint == null) {//得到乘客的起始坐标点_startPoint = new LatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());}//设置乘客源地址信息_tv_srcAddr.setText(aMapLocation.getAddress());_city = aMapLocation.getCity();}else {//定位失败,Log.e("Amap", "location error, code = "+ aMapLocation.getErrorCode()+", info = "+ aMapLocation.getErrorInfo());}}}});//3 开启定位服务_amapLocationClient.startLocation();}//绘制驾驶交通路径protected void drawRouteLine(){//1 创建路径的绘制的句柄 routeSearchRouteSearch routeSearch = new RouteSearch(getApplicationContext());RouteSearch.FromAndTo ft = new RouteSearch.FromAndTo(_startPoint,_endPoint);// 2 设置一个路径搜索的queryRouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(ft, RouteSearch.DrivingDefault,null, null, "");// 3 给绘制路径的句柄设置一个callback函数routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {@Overridepublic void onBusRouteSearched(BusRouteResult busRouteResult, int i) {//公交处理业务}@Overridepublic void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {//驾驶的理业务//判断是否请求成功if (i != 1000) {Log.e("Amap", "搜索驾驶路径失败");return ;}//画出驾驶路径//1 拿到得到的路径 (默认以第一个方案为优选方案)DrivePath path = driveRouteResult.getPaths().get(0);//驾驶路径一个覆盖物DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(getApplicationContext(),_amap,path,_startPoint,_endPoint);//先把之前的路径删除掉_amap.clear();//以适当的缩放显示路径drivingRouteOverlay.zoomToSpan();//去掉中间转弯的一些图标提示drivingRouteOverlay.setNodeIconVisibility(false);drivingRouteOverlay.setThroughPointIconVisibility(false);//将路径添加到地图drivingRouteOverlay.addToMap();}@Overridepublic void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {//步行的理业务}@Overridepublic void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {//骑行的理业务}});//3 启动路径所有 将query穿进去 向服务器发送请求routeSearch.calculateDriveRouteAsyn(query);}//开启POI兴趣点搜索protected void doSerarchPOI() {_bt_startOrder.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//开始搜索POI兴趣点Log.e("Amap", "button onclick");//拿到用户搜索地址的关键字String dstAddr = _attv_dstAddr.getText().toString();//开始POI搜索// 1 创建一个搜索的条件对象 queryPoiSearch.Query query = new PoiSearch.Query(dstAddr, "", _city);// 2 创建一个POISearch句柄和query关联PoiSearch poiSearch = new PoiSearch(getApplicationContext(), query);// 3 给search绑定一个回调函数poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {@Overridepublic void onPoiSearched(PoiResult poiResult, int i) {//处理得到的POI兴趣点集合 poiResultif (i != 1000) {Log.e("Amap", "poi Search error code = "+ i);return ;}//搜索成功List<PoiItem> poiList = poiResult.getPois();for (int index = 0; index < poiList.size(); index++) {//此时 表示处理每个已经搜索到的兴趣点Log.e("Amap", "搜索到的兴趣点有");PoiItem item = poiList.get(index);Log.e("Amap", "poi title =" + item.getTitle()+"latitude = " +item.getLatLonPoint().getLatitude()+"longitude = "+ item.getLatLonPoint().getLongitude());//给每个搜索到的标记点画一个标记addMarkerToMap(item.getLatLonPoint().getLatitude(),item.getLatLonPoint().getLongitude());//默认以第一个兴趣点为我们坐标点_endPoint = new LatLonPoint(item.getLatLonPoint().getLatitude(),item.getLatLonPoint().getLongitude());drawRouteLine();if (index == 0) {break;}}}@Overridepublic void onPoiItemSearched(PoiItem poiItem, int i) {}});// 4 启动搜索poiSearch.searchPOIAsyn();}});}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_passenger);initUI();createMap(savedInstanceState);doLocation();}}
参考:/api/android-sdk/guide/route-plan/drive