地图路径规划

司马璞
2023-12-01

1.添加依赖

	//3D地图so及jar
	compile 'com.amap.api:3dmap:latest.integration'
	//定位功能
	compile 'com.amap.api:location:latest.integration'
	//搜索功能
	compile 'com.amap.api:search:latest.integration'
 
2.manifests里面
        ①添加权限        
	<!-- 允许程序打开网络套接字 -->
	<uses-permission android:name="android.permission.INTERNET" />
	<!-- 允许程序设置内置sd卡的写权限 -->
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<!-- 允许程序获取网络状态 -->
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<!-- 允许程序访问WiFi网络信息 -->
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
	<!-- 允许程序读写手机状态和身份 -->
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<!-- 允许程序访问CellID或WiFi热点来获取粗略的位置 -->
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      ②Application里面添加      
	<meta-data
    	   android:name="com.amap.api.v2.apikey"
    	   android:value="2d323751100a9fec3cdfeec0422ed762">
   	 //key值每个项目就要去申请一个
	</meta-data>

3.复制sdk文件

4.MainActivity里面

   ①private一些用到的值

    //开始位置和结束位置的字符串
      private String startPlace, endPlace;
      //查询你自己输入的路径的
      private GeocodeSearch searchStart;
      private GeocodeSearch searchEnd;
      //这两个类是用来得到经纬度
    private LatLonPoint latLonPointStart;
    private LatLonPoint latLonPointEnd;

   ②initview下面

        searchStart = new GeocodeSearch(this);
        searchEnd = new GeocodeSearch(this);

   ③onClick里面    

           startPlace = start_et.getText().toString();

    endPlace = stop_et.getText().toString();
    if (startPlace.isEmpty() || endPlace.isEmpty()) {
         Toast.makeText(this, "请重新输入地址", Toast.LENGTH_SHORT).show();
     } else {
     //初始化开始位置的经纬度
      initStart();
    }
④定义方法initStart
	private void initStart() {
    	GeocodeQuery query = new GeocodeQuery(startPlace, "");
    	//查询你输入的字符串
    	searchStart.getFromLocationNameAsyn(query);
    	searchStart.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
       	 @Override
       	 public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        	}
         @Override
       	 public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            GeocodeAddress addr = geocodeResult.getGeocodeAddressList().get(0);
            latLonPointStart = addr.getLatLonPoint();
            Log.e("start", latLonPointStart.toString());
            initEnd();
          }
       });
     }

    ⑤定义方法initEnd
	//初始化终点路径
	private void initEnd() {
    	GeocodeQuery query1 = new GeocodeQuery(endPlace, "");
    	searchEnd.getFromLocationNameAsyn(query1);
    	searchEnd.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
        	@Override
        	public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        	}
        	@Override
        	public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            	GeocodeAddress addr = geocodeResult.getGeocodeAddressList().get(0);
            	latLonPointEnd = addr.getLatLonPoint();
            	Log.e("end", latLonPointEnd.toString());

            	if (latLonPointEnd == null || latLonPointStart == null) {
                	Toast.makeText(MainActivity.this, "正在异步处理", Toast.LENGTH_SHORT).show();
            	} else {
                	Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                	Bundle bundle = new Bundle();
                	//序列化的第二种方式
                	bundle.putParcelable("start", latLonPointStart);
                	bundle.putParcelable("end", latLonPointEnd);
                	intent.putExtras(bundle);
                	startActivity(intent);
            	}
        	}
    	});
	}
5.第二个Activity
     ①定义Private用到的
            private MapView mmap = null;
    private LatLonPoint start;
    private LatLonPoint end;
    private AMap aMap;
    private RouteSearch routeSearch;

      ②initview下面    

mmap.onCreate(savedInstanceState);

	if (aMap == null) {
    	aMap = mmap.getMap();
	}
	start = getIntent().getExtras().getParcelable("start");
	end = getIntent().getExtras().getParcelable("end");
	Log.e("homeActivity开始", start.toString());
	Log.e("homeActivity结束", end.toString());
	routeSearch = new RouteSearch(this);
	routeSearch.setRouteSearchListener(this);
	RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start, end);
	// fromAndTo包含路径规划的起点和终点,drivingMode表示驾车模式
	// 第三个参数表示途经点(最多支持16个),第四个参数表示避让区域(最多支持32个),第五个参数表示避让道路
	RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");
	routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询

     ③实现接口
        implements   RouteSearch.OnRouteSearchListener
        出来四个方法
    ④在onDriveRouteSearched方法里面添加
	aMap.clear();// 清理地图上的所有覆盖物
	final DrivePath drivePath = driveRouteResult.getPaths()
        	.get(0);
	DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
        	HomeActivity.this, aMap, drivePath,
        	driveRouteResult.getStartPos(),
        	driveRouteResult.getTargetPos(), null);
	drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
	drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
	drivingRouteOverlay.removeFromMap();
	drivingRouteOverlay.addToMap();
	drivingRouteOverlay.zoomToSpan();
⑤在onCreat下面添加
 
	@Override
	protected void onDestroy() {
    	super.onDestroy();
    	//在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
    	mmap.onDestroy();
	}

	@Override
	protected void onResume() {
    	super.onResume();
    	//在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
    	mmap.onResume();
	}

	@Override
	protected void onPause() {
    	super.onPause();
    	//在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
    	mmap.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
    	mmap.onSaveInstanceState(outState);
	}
 类似资料: