先看一下效果圖:
1、剛進入時:,預設為北京
2、點擊“bus”按鈕:
3、點擊”car”按鈕:
4、點擊”walk”按鈕
好了,接下來開始實現吧!
布局:<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>
<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<Button
android:id=”@+id/btn_bus”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:onClick=”doClick”
android:text=”bus” />
<Button
android:id=”@+id/btn_car”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:onClick=”doClick”
android:text=”car” />
<Button
android:id=”@+id/btn_walk”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:onClick=”doClick”
android:text=”walk” />
</LinearLayout>
<com.amap.api.maps.MapView
android:id=”@+id/map”
android:layout_width=”match_parent”
android:layout_height=”match_parent”></com.amap.api.maps.MapView>
</LinearLayout>
功能:
package cn.zmit.xianneng.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
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.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import cn.zmit.xianneng.R;
/**
* Created by kyle_zheng on 2016/1/20 0020.
*/
public class TestActivity extends Activity implements RouteSearch.OnRouteSearchListener {
MapView mapView;
AMap aMap;
RouteSearch.FromAndTo fromAndTo;//起始點和終點的經緯度
RouteSearch routeSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
if (aMap == null) {
aMap = mapView.getMap();
}
LatLonPoint start = new LatLonPoint(31.286389, 118.378039);//起點為安師大新校區的經緯度
LatLonPoint end = new LatLonPoint(31.288702, 118.360532);//終點為皖南醫學院的經緯度
fromAndTo = new RouteSearch.FromAndTo(start, end);//執行個體化FromAndTo,字面意思,哪到哪
routeSearch = new RouteSearch(this);//執行個體化routeSearch
routeSearch.setRouteSearchListener(this);
}
public void doClick(View view) {
switch (view.getId()) {
case R.id.btn_bus:
RouteSearch.BusRouteQuery busRouteQuery = new RouteSearch.BusRouteQuery(fromAndTo, RouteSearch.BusDefault, “蕪湖”, 0);// 第一個參數表示路徑規劃的起點和終點,第二個參數表示公交查詢模式,第三個參數表示公交查詢城市區號,第四個參數表示是否計算夜班車,0表示不計算
routeSearch.calculateBusRouteAsyn(busRouteQuery);
break;
case R.id.btn_car:
RouteSearch.DriveRouteQuery driveRouteQuery = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, “”);
routeSearch.calculateDriveRouteAsyn(driveRouteQuery);
break;
case R.id.btn_walk:
RouteSearch.WalkRouteQuery walkRouteQuery = new RouteSearch.WalkRouteQuery(fromAndTo, RouteSearch.WalkDefault);
routeSearch.calculateWalkRouteAsyn(walkRouteQuery);
break;
}
}
@Override
public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
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();
}
@Override
public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
DrivePath drivePath = driveRouteResult.getPaths().get(0);
aMap.clear();
DrivingRouteOverlay routeOverlay = new DrivingRouteOverlay(this, aMap,
drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());
routeOverlay.removeFromMap();
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
}
@Override
public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
WalkPath walkPath = walkRouteResult.getPaths().get(0);//取其中一個路線
aMap.clear();//清除地圖上的標註之類
WalkRouteOverlay routeOverlay = new WalkRouteOverlay(this, aMap,
walkPath, walkRouteResult.getStartPos(),
walkRouteResult.getTargetPos());
routeOverlay.removeFromMap();
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
}
//以下要複寫
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
好了,over,覺得有用的點個贊吧~