Android Studio 百度地圖導航

程式碼如風~~~發表於2021-08-31

   配置就不再多說了,上一篇已經詳細說過了,這次就是根據經緯度座標做地圖導航,路徑規劃。直接上程式碼:

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); } } }

  ,

相關文章