1、配置路线规划的资源(提取码:84la)
(1).复制assets里面的文件到项目中的assets中
(2).values中的styles.xml替换项目的styles.xml
(3).在下载的com文件中找到“baidu”文件夹,复制到项目中的java/com文件下
(4).在下载的layout中找到“text_bubble.xml”文件,复制放到项目中的layout下
2、在布局文件中加入新的路线规划按钮
<Button android:id="@+id/btn_Plan" android:text="规划" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="60dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />
3、MainActivity代码
package com.example.baidumap; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.CoordType; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.overlayutil.WalkingRouteOverlay; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.route.BikingRouteResult; 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.WalkingRoutePlanOption; import com.baidu.mapapi.search.route.WalkingRouteResult; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private MapView mMapView = null; private BaiduMap mBaiduMap = null; private Context context; //定位相关 private double mLatitude; private double mLongtitude; //方向传感器 private MyOrientationListener mMyOrientationListener; private float mCurrentX; //自定义图标 private BitmapDescriptor mIconLocation; private LocationClient mLocationClient; public BDAbstractLocationListener myListener; private LatLng mLastLocationData; private boolean isFirstin = true; // 路线规划相关 private RoutePlanSearch mSearch = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); SDKInitializer.setCoordType(CoordType.BD09LL); this.context = this; mMapView = (MapView) findViewById(R.id.baiduMapView); //获取地图控件引用 mBaiduMap = mMapView.getMap(); initMyLocation(); initPoutePlan(); button(); } protected void onStart() { super.onStart(); //开启定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) mLocationClient.start(); //开启方向传感器 mMyOrientationListener.start(); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onStop() { super.onStop(); //停止定位 mBaiduMap.setMyLocationEnabled(false); mLocationClient.stop(); //停止方向传感器 mMyOrientationListener.stop(); } @Override protected void onDestroy() { super.onDestroy(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; mSearch.destroy(); } @Override public void onClick(View v) { SDKInitializer.initialize(getApplicationContext()); switch (v.getId()) { case R.id.btn_Location: { centerToMyLocation(mLatitude, mLongtitude); break; } case R.id.btn_Plan: { StarRoute(); break; } } } //按钮响应 private void button() { //按钮 Button mbut_Loc = (Button) findViewById(R.id.btn_Location); Button mbut_RoutrPlan = (Button) findViewById(R.id.btn_Plan); //按钮处理 mbut_Loc.setOnClickListener(this); mbut_RoutrPlan.setOnClickListener(this); } //定位 private class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapView 销毁后不在处理新接收的位置 if (location == null || mMapView == null){ return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(mCurrentX).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); //设置自定义图标 MyLocationConfiguration config = new MyLocationConfiguration( MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation); mBaiduMap.setMyLocationConfiguration(config); //更新经纬度 mLatitude = location.getLatitude(); mLongtitude = location.getLongitude(); //设置起点 mLastLocationData = new LatLng(mLatitude, mLongtitude); if (isFirstin) { centerToMyLocation(location.getLatitude(), location.getLongitude()); if (location.getLocType() == BDLocation.TypeGpsLocation) { // GPS定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { // 网络定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) { // 离线定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeServerError) { Toast.makeText(context, "定位:服务器错误", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { Toast.makeText(context, "定位:网络错误", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { Toast.makeText(context, "定位:手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show(); } isFirstin = false; } } } //初始化定位 private void initMyLocation() { //缩放地图 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); mBaiduMap.setMapStatus(msu); //开启定位 mBaiduMap.setMyLocationEnabled(true); //声明LocationClient类 mLocationClient = new LocationClient(this); //通过LocationClientOption设置LocationClient相关参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setIsNeedAddress(true);//设置是否需要地址信息 option.setScanSpan(1000); //设置locationClientOption mLocationClient.setLocOption(option); myListener = new MyLocationListener(); //注册监听函数 mLocationClient.registerLocationListener(myListener); //初始化图标 mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps); initOrientation(); //开始定位 mLocationClient.start(); } //回到定位中心 private void centerToMyLocation(double latitude, double longtitude) { mBaiduMap.clear(); mLastLocationData = new LatLng(latitude, longtitude); MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mLastLocationData); mBaiduMap.animateMapStatus(msu); } //传感器 private void initOrientation() { //传感器 mMyOrientationListener = new MyOrientationListener(context); mMyOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mCurrentX = x; } }); } //路线规划初始化 private void initPoutePlan() { mSearch = RoutePlanSearch.newInstance(); mSearch.setOnGetRoutePlanResultListener(listener); } // 路线规划模块 public OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } @Override public void onGetTransitRouteResult(TransitRouteResult var1) { } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult var1) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult result) { } @Override public void onGetIndoorRouteResult(IndoorRouteResult var1) { } @Override public void onGetBikingRouteResult(BikingRouteResult var1) { } }; //开始规划 private void StarRoute() { SDKInitializer.initialize(getApplicationContext()); // 设置起、终点信息 PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "西二旗地铁站"); PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "百度科技园"); mSearch.walkingSearch((new WalkingRoutePlanOption()) .from(stNode) .to(enNode)); } }
4、运行项目
5、附上项目(提取码:52p3)(下载下来的项目点击规划,会出现如下情况)
代码段报:
6、原因是因为AndroidManifest.xml中没有key,新建一个项目,拿到SHA1,到百度控制台新建一个应用,获取key即可。
<---------以上代码是静态规划路线-----------以下代码是动态规划路线-------------------->
1.新增加了公交规划、输入地址规划、静态经纬度规划,在原来的基础上加代码(MainActivity代码):
package com.example.baidumap; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.CoordType; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.overlayutil.BikingRouteOverlay; import com.baidu.mapapi.overlayutil.DrivingRouteOverlay; import com.baidu.mapapi.overlayutil.MassTransitRouteOverlay; import com.baidu.mapapi.overlayutil.TransitRouteOverlay; import com.baidu.mapapi.overlayutil.WalkingRouteOverlay; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.route.BikingRoutePlanOption; import com.baidu.mapapi.search.route.BikingRouteResult; 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.MassTransitRoutePlanOption; 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.TransitRoutePlanOption; import com.baidu.mapapi.search.route.TransitRouteResult; import com.baidu.mapapi.search.route.WalkingRoutePlanOption; import com.baidu.mapapi.search.route.WalkingRouteResult; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private MapView mMapView = null; private BaiduMap mBaiduMap = null; private Context context; //定位相关 private double mLatitude; private double mLongtitude; //方向传感器 private MyOrientationListener mMyOrientationListener; private float mCurrentX; //自定义图标 private BitmapDescriptor mIconLocation; private LocationClient mLocationClient; public BDAbstractLocationListener myListener; private LatLng mLastLocationData; private boolean isFirstin = true; // 路线规划相关 private RoutePlanSearch mSearch = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); SDKInitializer.setCoordType(CoordType.BD09LL); this.context = this; mMapView = (MapView) findViewById(R.id.baiduMapView); //获取地图控件引用 mBaiduMap = mMapView.getMap(); initMyLocation(); initPoutePlan(); button(); } protected void onStart() { super.onStart(); //开启定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) mLocationClient.start(); //开启方向传感器 mMyOrientationListener.start(); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onStop() { super.onStop(); //停止定位 mBaiduMap.setMyLocationEnabled(false); mLocationClient.stop(); //停止方向传感器 mMyOrientationListener.stop(); } @Override protected void onDestroy() { super.onDestroy(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; mSearch.destroy(); } @Override public void onClick(View v) { SDKInitializer.initialize(getApplicationContext()); switch (v.getId()) { case R.id.btn_Location: { centerToMyLocation(mLatitude, mLongtitude); break; } case R.id.btn_Plan: { Intent intent = new Intent(context,RoutePlanActivity.class); startActivityForResult(intent,0x11); break; } } } //按钮响应 private void button() { //按钮 Button mbut_Loc = (Button) findViewById(R.id.btn_Location); Button mbut_RoutrPlan = (Button) findViewById(R.id.btn_Plan); //按钮处理 mbut_Loc.setOnClickListener(this); mbut_RoutrPlan.setOnClickListener(this); } //定位 private class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapView 销毁后不在处理新接收的位置 if (location == null || mMapView == null){ return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(mCurrentX).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); //设置自定义图标 MyLocationConfiguration config = new MyLocationConfiguration( MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation); mBaiduMap.setMyLocationConfiguration(config); //更新经纬度 mLatitude = location.getLatitude(); mLongtitude = location.getLongitude(); //设置起点 mLastLocationData = new LatLng(mLatitude, mLongtitude); if (isFirstin) { centerToMyLocation(location.getLatitude(), location.getLongitude()); if (location.getLocType() == BDLocation.TypeGpsLocation) { // GPS定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { // 网络定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) { // 离线定位结果 Toast.makeText(context, "定位:"+location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeServerError) { Toast.makeText(context, "定位:服务器错误", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { Toast.makeText(context, "定位:网络错误", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { Toast.makeText(context, "定位:手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show(); } isFirstin = false; } } } //初始化定位 private void initMyLocation() { //缩放地图 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); mBaiduMap.setMapStatus(msu); //开启定位 mBaiduMap.setMyLocationEnabled(true); //声明LocationClient类 mLocationClient = new LocationClient(this); //通过LocationClientOption设置LocationClient相关参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setIsNeedAddress(true);//设置是否需要地址信息 option.setScanSpan(1000); //设置locationClientOption mLocationClient.setLocOption(option); myListener = new MyLocationListener(); //注册监听函数 mLocationClient.registerLocationListener(myListener); //初始化图标 mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps); initOrientation(); //开始定位 mLocationClient.start(); } //回到定位中心 private void centerToMyLocation(double latitude, double longtitude) { mBaiduMap.clear(); mLastLocationData = new LatLng(latitude, longtitude); MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(mLastLocationData); mBaiduMap.animateMapStatus(msu); } //传感器 private void initOrientation() { //传感器 mMyOrientationListener = new MyOrientationListener(context); mMyOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mCurrentX = x; } }); } //路线规划初始化 private void initPoutePlan() { mSearch = RoutePlanSearch.newInstance(); mSearch.setOnGetRoutePlanResultListener(listener); } // 路线规划模块 public OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } @Override public void onGetTransitRouteResult(TransitRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); TransitRouteOverlay overlay = new TransitRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); MassTransitRouteOverlay overlay = new MassTransitRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } @Override public void onGetDrivingRouteResult(DrivingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } @Override public void onGetIndoorRouteResult(IndoorRouteResult var1) { } @Override public void onGetBikingRouteResult(BikingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "路线规划:未找到结果,检查输入", Toast.LENGTH_SHORT).show(); //禁止定位 isFirstin = false; } assert result != null; if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息 result.getSuggestAddrInfo(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); Toast.makeText(MainActivity.this, "路线规划:搜索完成", Toast.LENGTH_SHORT).show(); BikingRouteOverlay overlay = new BikingRouteOverlay(mBaiduMap); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } //禁止定位 isFirstin = false; } }; //开始规划 private void StarRoute(int id,ArrayList list) { SDKInitializer.initialize(getApplicationContext()); //经纬度规划路线和动态输入规划路线二选一 // 设置起、终点信息 动态输入规划路线 PlanNode stNode = PlanNode.withCityNameAndPlaceName(list.get(0).toString(), list.get(1).toString()); PlanNode enNode = PlanNode.withCityNameAndPlaceName(list.get(2).toString(), list.get(3).toString()); //经纬度规划路线 // LatLng startPoint = new LatLng(39.917380, 116.37978); // LatLng endPoint = new LatLng(22.27503, 113.523431); // PlanNode stNode = PlanNode.withLocation(startPoint); // PlanNode enNode = PlanNode.withLocation(endPoint); switch (id){ case 1: mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(enNode)); break; case 2: if(list.get(0).toString() == list.get(2).toString()){ mSearch.transitSearch((new TransitRoutePlanOption()).from(stNode).to(enNode)); }else{ mSearch.masstransitSearch((new MassTransitRoutePlanOption()).from(stNode).to(enNode)); } break; default:break; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 0x11){ ArrayList address = data.getCharSequenceArrayListExtra("address"); StarRoute(resultCode,address); } } }
2.跳转到规划路线输入框的代码页(RoutePlanActivity):
package com.example.baidumap; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class RoutePlanActivity extends AppCompatActivity implements View.OnClickListener{ private Button btnDrive,btnBus; private EditText start_edt_city,start_edt_address,end_edt_city,end_edt_address; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_route_plan); initView(); } private void initView(){ btnDrive = (Button)findViewById(R.id.btn_drive); btnBus = (Button)findViewById(R.id.btn_bus); start_edt_city = (EditText)findViewById(R.id.Start_Edt_City); start_edt_address = (EditText)findViewById(R.id.Start_Edt_Address); end_edt_city = (EditText)findViewById(R.id.End_Edt_City); end_edt_address = (EditText)findViewById(R.id.End_Edt_Address); btnDrive.setOnClickListener(this); btnBus.setOnClickListener(this); }; @Override public void onClick(View view) { String startCity = start_edt_city.getText().toString(); String startAddress = start_edt_address.getText().toString(); String endCity = end_edt_city.getText().toString(); String endAddress = end_edt_address.getText().toString(); ArrayList<String> addressList = new ArrayList<>(); addressList.add(startCity); addressList.add(startAddress); addressList.add(endCity); addressList.add(endAddress); if(startCity.isEmpty() || startAddress.isEmpty() || endCity.isEmpty() || endAddress.isEmpty()){ Toast.makeText(this, "起点和终点不能为空" , Toast.LENGTH_SHORT).show(); }else { Intent intent = new Intent(); intent.putStringArrayListExtra("address",addressList); switch (view.getId()) { case R.id.btn_drive: setResult(1,intent); break; case R.id.btn_bus: setResult(2,intent); break; } finish(); } } }
3.RoutePlanActivity的布局页面代码(activity_route_plan):
package com.example.baidumap; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class RoutePlanActivity extends AppCompatActivity implements View.OnClickListener{ private Button btnDrive,btnBus; private EditText start_edt_city,start_edt_address,end_edt_city,end_edt_address; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_route_plan); initView(); } private void initView(){ btnDrive = (Button)findViewById(R.id.btn_drive); btnBus = (Button)findViewById(R.id.btn_bus); start_edt_city = (EditText)findViewById(R.id.Start_Edt_City); start_edt_address = (EditText)findViewById(R.id.Start_Edt_Address); end_edt_city = (EditText)findViewById(R.id.End_Edt_City); end_edt_address = (EditText)findViewById(R.id.End_Edt_Address); btnDrive.setOnClickListener(this); btnBus.setOnClickListener(this); }; @Override public void onClick(View view) { String startCity = start_edt_city.getText().toString(); String startAddress = start_edt_address.getText().toString(); String endCity = end_edt_city.getText().toString(); String endAddress = end_edt_address.getText().toString(); ArrayList<String> addressList = new ArrayList<>(); addressList.add(startCity); addressList.add(startAddress); addressList.add(endCity); addressList.add(endAddress); if(startCity.isEmpty() || startAddress.isEmpty() || endCity.isEmpty() || endAddress.isEmpty()){ Toast.makeText(this, "起点和终点不能为空" , Toast.LENGTH_SHORT).show(); }else { Intent intent = new Intent(); intent.putStringArrayListExtra("address",addressList); switch (view.getId()) { case R.id.btn_drive: setResult(1,intent); break; case R.id.btn_bus: setResult(2,intent); break; } finish(); } } }
4.最后一步,如果整个代码复制过去的话(自个创建可忽略),要在AndroidManifest.xml文件中加上如下代码:
<activity android:name=".RoutePlanActivity"></activity>
5.启动项目(例图):