android百度地图路线查询,Android百度地图——路线规划搜索
百度地圖提供的路線規劃搜索有3種:駕車、公交、步行。
3中方式的使用方法類似,步驟如下:
1、初始化地圖
/**初始化百度地圖
*
*/
private void initBaiduMap(){
//初始化地圖
mMapView.showZoomControls(false);
mBaidumap = mMapView.getMap();
//地圖點擊事件處理
mBaidumap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onMapClick(LatLng arg0) {
// TODO Auto-generated method stub
mBaidumap.hideInfoWindow();
}
});
// 初始化搜索模塊,注冊事件監聽
mSearch = RoutePlanSearch.newInstance();
//mSearch.setOnGetRoutePlanResultListener(this);
mBaidumap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// TODO Auto-generated method stub
//地圖加載完成
SearchButtonProcess();//調用路徑規劃
}
});
}
2、規劃路線,包括設置起始點、終點、途經點,并選擇一種搜索方式。
/**
* 發起路線規劃搜索示例
*
* @param v
*/
private void SearchButtonProcess() {
//重置瀏覽節點的路線數據
//route = null;
mBaidumap.clear();
ArrayList arg0 =new ArrayList();
//設置起終點、途經點信息,對于tranist search 來說,城市名無意義
PlanNode stNode = PlanNode.withLocation(new LatLng(StationsList.getStationsList().get(0).getLatitude(), StationsList.getStationsList().get(0).getLongitude()));
PlanNode enNode = PlanNode.withLocation(new LatLng(StationsList.getStationsList().get(StationsList.getStationsList().size()-1).getLatitude(), StationsList.getStationsList().get(StationsList.getStationsList().size()-1).getLongitude()));
for (int i = 1; i < StationsList.getStationsList().size()-1; i++) {
PlanNode node = PlanNode.withLocation(new LatLng(StationsList.getStationsList().get(i).getLatitude(), StationsList.getStationsList().get(i).getLongitude()));
arg0.add(node);
}
// 實際使用中請對起點終點城市進行正確的設定
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(stNode)//起點
.passBy(arg0)//途經點
.to(enNode));//終點
}
3、接收并處理結果。通常需要集合覆蓋物來顯示路線。
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(activity, "抱歉,未找到結果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起終點或途經點地址有岐義,通過以下接口獲取建議查詢信息
//result.getSuggestAddrInfo()
Toast.makeText(activity, "抱歉,未找到結果", Toast.LENGTH_SHORT).show();
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
// route = result.getRouteLines().get(0);
DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaidumap);//路線覆蓋物,MyDrivingRouteOverlay代碼下面給出
routeOverlay = overlay;
// mBaidumap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
}
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult result) {
}
路線覆蓋物類MyDrivingRouteOverlay:
//定制RouteOverly
private class MyDrivingRouteOverlay extends DrivingRouteOverlay {
public MyDrivingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap,StationsList.getStationsList().size());
}
@Override
public BitmapDescriptor getStartMarker() {
//覆寫此方法以改變默認起點圖標
return BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);
//這里可以使用BitmapDescriptorFactory.fromView(view)實現自定義view覆蓋物,自定義覆蓋物請關注以后的文章。
}
@Override
public int getLineColor() {
// TODO Auto-generated method stub
//覆寫此方法以改變默認繪制顏色
//Returns:
//線顏色
return super.getLineColor();
}
@Override
public BitmapDescriptor getTerminalMarker() {
//覆寫此方法以改變默認終點圖標
return BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);
}
@Override
public boolean onRouteNodeClick(int i) {
// TODO Auto-generated method stub
//覆寫此方法以改變默認點擊處理
return true;
}
}
DrivingRouteOverlay是百度提供的源碼,可以去官網下載,源碼如下:
package com.baidu.mapapi.overlayutil;
import android.R.integer;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRouteLine.DrivingStep;
import java.util.ArrayList;
import java.util.List;
/**
* 用于顯示一條駕車路線的overlay,自3.4.0版本起可實例化多個添加在地圖中顯示,當數據中包含路況數據時,則默認使用路況紋理分段繪制
*/
public class DrivingRouteOverlay extends OverlayManager {
private int stationsize=0;
private DrivingRouteLine mRouteLine = null;
boolean focus = false;
/**
* 構造函數
*
* @param baiduMap
* 該DrivingRouteOvelray引用的 BaiduMap
*/
public DrivingRouteOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
public DrivingRouteOverlay(BaiduMap baiduMap,int size) {
super(baiduMap);
stationsize=size;
}
@Override
public final List getOverlayOptions() {
if (mRouteLine == null) {
return null;
}
List overlayOptionses = new ArrayList();
// step node
// if (mRouteLine.getAllStep() != null
// && mRouteLine.getAllStep().size() > 0) {
//
// for (DrivingRouteLine.DrivingStep step : mRouteLine.getAllStep()) {
// Bundle b = new Bundle();
// b.putInt("index", mRouteLine.getAllStep().indexOf(step));
// if (step.getEntrance() != null) {
// overlayOptionses.add((new MarkerOptions())
// .position(step.getEntrance().getLocation())
// .anchor(0.5f, 0.5f)
// .zIndex(10)
// .rotate((360 - step.getDirection()))
// .extraInfo(b)
// .icon(BitmapDescriptorFactory
// .fromAssetWithDpi("Icon_line_node.png")));
// }
// // 最后路段繪制出口點
// if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine
// .getAllStep().size() - 1) && step.getExit() != null) {
// overlayOptionses.add((new MarkerOptions())
// .position(step.getExit().getLocation())
// .anchor(0.5f, 0.5f)
// .zIndex(10)
// .icon(BitmapDescriptorFactory
// .fromAssetWithDpi("Icon_line_node.png")));
//
// }
// }
// }
if (mRouteLine.getStarting() != null) {
overlayOptionses.add((new MarkerOptions())
.position(mRouteLine.getStarting().getLocation())
.icon(getStartMarker() != null ? getStartMarker() :
BitmapDescriptorFactory
.fromAssetWithDpi("Icon_start.png")).zIndex(0));
}
if (mRouteLine.getTerminal() != null) {
overlayOptionses
.add((new MarkerOptions())
.position(mRouteLine.getTerminal().getLocation())
.icon(getTerminalMarker() != null ? getTerminalMarker() :
BitmapDescriptorFactory
.fromAssetWithDpi("Icon_end.png"))
.zIndex(stationsize-1));
}
// poly line
if (mRouteLine.getAllStep() != null
&& mRouteLine.getAllStep().size() > 0) {
List steps = mRouteLine.getAllStep();
int stepNum = steps.size();
List points = new ArrayList();
ArrayList traffics = new ArrayList();
int totalTraffic = 0;
for (int i = 0; i < stepNum ; i++) {
if (i == stepNum - 1) {
points.addAll(steps.get(i).getWayPoints());
} else {
points.addAll(steps.get(i).getWayPoints().subList(0, steps.get(i).getWayPoints().size() - 1));
}
totalTraffic += steps.get(i).getWayPoints().size() - 1;
if (steps.get(i).getTrafficList() != null && steps.get(i).getTrafficList().length > 0) {
for (int j = 0;j < steps.get(i).getTrafficList().length;j++) {
traffics.add(steps.get(i).getTrafficList()[j]);
}
}
}
// Bundle indexList = new Bundle();
// if (traffics.size() > 0) {
// int raffic[] = new int[traffics.size()];
// int index = 0;
// for (Integer tempTraff : traffics) {
// raffic[index] = tempTraff.intValue();
// index++;
// }
// indexList.putIntArray("indexs", raffic);
// }
boolean isDotLine = false;
if (traffics != null && traffics.size() > 0) {
isDotLine = true;
}
PolylineOptions option = new PolylineOptions().points(points).textureIndex(traffics)
.width(10).dottedLine(isDotLine).focus(true)
.color(getLineColor() != 0 ? getLineColor() : Color.argb(178, 0, 78, 255)).zIndex(0);
if (isDotLine) {
option.customTextureList(getCustomTextureList());
}
overlayOptionses.add(option);
}
return overlayOptionses;
}
/**
* 設置路線數據
*
* @param routeLine
* 路線數據
*/
public void setData(DrivingRouteLine routeLine) {
this.mRouteLine = routeLine;
}
/**
* 覆寫此方法以改變默認起點圖標
*
* @return 起點圖標
*/
public BitmapDescriptor getStartMarker() {
return null;
}
/**
* 覆寫此方法以改變默認繪制顏色
* @return 線顏色
*/
public int getLineColor() {
return 0;
}
public List getCustomTextureList() {
ArrayList list = new ArrayList();
list.add(BitmapDescriptorFactory.fromAsset("Icon_road_blue_arrow.png"));
list.add(BitmapDescriptorFactory.fromAsset("Icon_road_green_arrow.png"));
list.add(BitmapDescriptorFactory.fromAsset("Icon_road_yellow_arrow.png"));
list.add(BitmapDescriptorFactory.fromAsset("Icon_road_red_arrow.png"));
list.add(BitmapDescriptorFactory.fromAsset("Icon_road_nofocus.png"));
return list;
}
/**
* 覆寫此方法以改變默認終點圖標
*
* @return 終點圖標
*/
public BitmapDescriptor getTerminalMarker() {
return null;
}
/**
* 覆寫此方法以改變默認點擊處理
*
* @param i
* 線路節點的 index
* @return 是否處理了該點擊事件
*/
public boolean onRouteNodeClick(int i) {
if (mRouteLine.getAllStep() != null
&& mRouteLine.getAllStep().get(i) != null) {
Log.i("baidumapsdk", "DrivingRouteOverlay onRouteNodeClick");
}
return false;
}
@Override
public final boolean onMarkerClick(Marker marker) {
for (Overlay mMarker : mOverlayList) {
if (mMarker instanceof Marker && mMarker.equals(marker)) {
if (marker.getExtraInfo() != null) {
onRouteNodeClick(marker.getExtraInfo().getInt("index"));
}
}
}
return true;
}
@Override
public boolean onPolylineClick(Polyline polyline) {
boolean flag = false;
for (Overlay mPolyline : mOverlayList) {
if (mPolyline instanceof Polyline && mPolyline.equals(polyline)) {
// 選中
flag = true;
break;
}
}
setFocus(flag);
return true;
}
public void setFocus(boolean flag) {
focus = flag;
for (Overlay mPolyline : mOverlayList) {
if (mPolyline instanceof Polyline) {
// 選中
((Polyline) mPolyline).setFocus(flag);
break;
}
}
}
}
總結
以上是生活随笔為你收集整理的android百度地图路线查询,Android百度地图——路线规划搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript的三级联动
- 下一篇: android sina oauth2.