Android Studio 百度地图导航
配置就不再多说了,上一篇已经详细说过了,这次就是根据经纬度坐标做地图导航,路径规划。直接上代码:
package com.example.appview.mian_page.Frament.Anzhuang_Frament.ItemA_Activity; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; 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.model.LatLng; import com.baidu.mapapi.utils.route.BaiduMapRoutePlan; import com.baidu.mapapi.utils.route.RouteParaOption; import com.example.appview.R; import com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Activity.Preject_ItemA_AlterJZ; import com.google.android.gms.maps.MapView; import com.hjq.bar.OnTitleBarListener; import java.util.ArrayList; import java.util.List; public class ItemA_Nav_Activity extends AppCompatActivity { double locationX; double locationy; public LocationClient mLocationClient = null; private MapView mapView; // private MyLocationListener myListener = new MyLocationListener(); @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); SDKInitializer.setCoordType(CoordType.BD09LL); setContentView(R.layout.activity_item_anav); mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(new MyLocationListener()); getPermissionMethod(); getlocation(); LatLng startPoint = new LatLng(39.915291, 116.403857); LatLng endPoint = new LatLng(40.056858, 116.308194); //构建RouteParaOption参数以及策略 //也可以通过startName和endName来构造 RouteParaOption paraOption = new RouteParaOption() .startPoint(startPoint) .endPoint(endPoint) .busStrategyType(RouteParaOption.EBusStrategyType.bus_recommend_way); //调起百度地图 try { BaiduMapRoutePlan.openBaiduMapTransitRoute(paraOption, this); } catch (Exception e) { e.printStackTrace(); } //调起结束时及时调用finish方法以释放相关资源 BaiduMapRoutePlan.finish(this); } private void getlocation(){ LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setAddrType("all"); mLocationClient.setLocOption(option); mLocationClient.start(); //mLocationClient为第二步初始化过的LocationClient对象 //需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用 //更多LocationClientOption的配置,请参照类参考中LocationClientOption类的详细说明 }
//这个是根据获取当前经纬度坐标,然后到指定坐标做导航。上面直接定义了两个坐标做测试。 public class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location){ //此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果 //以下只列举部分获取经纬度相关(常用)的结果信息 //更多结果信息获取说明,请参照类参考中BDLocation类中的说明 double latitude = location.getLatitude(); //获取纬度信息 double longitude = location.getLongitude(); //获取经度信息 //float radius = location.getRadius(); //获取定位精度,默认值为0.0f //String coorType = location.getCoorType(); //获取经纬度坐标类型,以LocationClientOption中设置过的坐标类型为准 int errorCode = location.getLocType(); //获取定位类型、定位错误返回码,具体信息可参照类参考中BDLocation类中的说明 //此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果 //以下只列举部分获取地址相关的结果信息 //更多结果信息获取说明,请参照类参考中BDLocation类中的说明 //String addr = location.getAddrStr(); //获取详细地址信息 String country = location.getCountry(); //获取国家 String province = location.getProvince(); //获取省份 String city = location.getCity(); //获取城市 String district = location.getDistrict(); //获取区县 String street = location.getStreet(); //获取街道信息 //String adcode = location.getAdCode(); //获取adcode String town = location.getTown(); //获取乡镇信息 String str=country+"-"+province+"-"+city+"-"+district+"-"+street+"-"+town; locationX=latitude; locationy=longitude; } } // 权限请求 private void getPermissionMethod() { List<String> permissionList = new ArrayList<>(); if(ContextCompat.checkSelfPermission(ItemA_Nav_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (!permissionList.isEmpty()){ //权限列表不是空 String[] permissions = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(ItemA_Nav_Activity.this,permissions,1); } } }
,