百度地图三(获取地图路线规划)
actitvity.java中
package com.example.lgkj.bugmapapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
public class RouteMapActivity extends Activity {
private int totalpages = 0;
private ListView listView = null;
private RoutePlanSearch routeSearch;
private int currentPage = 0;
ArrayAdapter<String> adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在使用SDK各组件之前初始化context信息,传入ApplicationContext
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_route_map);
listView = (ListView) findViewById(R.id.mylistView);
// 第一步,创建路线检索实例
routeSearch = RoutePlanSearch.newInstance();
// 第三步,设置路线检索监听者;
routeSearch.setOnGetRoutePlanResultListener(routeListener);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 第四步,准备检索起、终点信息;
PlanNode stNode = PlanNode.withCityNameAndPlaceName("开封", "河南大学新校区西门");
PlanNode enNode = PlanNode.withCityNameAndPlaceName("开封", "鼓楼广场");
// 第五步,发起驾车线路规划检索;
routeSearch.drivingSearch(new DrivingRoutePlanOption().from(stNode).to(enNode));
currentPage++;
if (currentPage == totalpages) {
currentPage = 0;
}
}
});
}
// 第二步,创建POI检索监听者;
OnGetRoutePlanResultListener routeListener = new OnGetRoutePlanResultListener() {
private String s = null;
@Override
public void onGetDrivingRouteResult(DrivingRouteResult drivingResult) {
// TaxiInfo taxiInfo = drivingResult.getTaxiInfo(); 已经弃用
List<String> datas = new ArrayList<String>();
List<DrivingRouteLine> routeLines = drivingResult.getRouteLines();
if (routeLines != null) {
for (DrivingRouteLine drivingRouteLine : routeLines) {
// 分别列出路线的第一步
datas.add(drivingRouteLine.getAllStep().get(0).getExitInstructions() + ",距离:"
+ drivingRouteLine.getDistance() / 1000.f + "千米,大约用时:" + drivingRouteLine.getDuration() / 60 + "分");
}
listView
.setAdapter(new ArrayAdapter<String>(RouteMapActivity.this, android.R.layout.simple_list_item_1, datas));
// SuggestAddrInfo suggestAddrInfo = drivingResult.getSuggestAddrInfo();
// 使用方法与上面类似
}
}
@Override
public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult transitResult) {
}
@Override
public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult walkingResult) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
// 第六步,释放检索实例;
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
routeSearch.destroy();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
}
}
xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<ListView
android:id="@+id/mylistView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
详情请看原文
https://blog.csdn.net/u013616976/article/details/45460519#