问题描述
- androi高德地图路径规划问题
-
有做过高德路径规划的大神吗?下面是我仿照的高德官方案例来写的代码,可是总是报错,然后错误码是45,不知道是怎么回事。
package com.example.maptest;import android.os.Bundle;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.WalkRouteResult;import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;public class MainActivity extends Activity implements OnRouteSearchListener{
private AMap aMap; private MapView mapView; private int drivingMode = RouteSearch.DrivingDefault; private DriveRouteResult driveRouteResult; private LatLonPoint startPoint; private LatLonPoint endPoint; private RouteSearch routeSearch; private Marker startMk, targetMk; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
// MapsInitializer.sdcardDir = OffLineMapUtils.getSdCacheDir(this);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写routeSearch = new RouteSearch(this); routeSearch.setRouteSearchListener(this); init(); startPoint = new LatLonPoint(114.2001098435, 22.9537733103); endPoint = new LatLonPoint(23.0231558804, 114.6609545055); searchRouteResult(startPoint, endPoint); } private void init() { if (aMap == null) { aMap = mapView.getMap(); } // 设置地图可视缩放大小
// aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
} @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** 095. * 方法必须重写 096. */ @Override protected void onPause() { super.onPause(); mapView.onPause(); } /** 104. * 方法必须重写 105. */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** 113. * 方法必须重写 114. */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } public void searchRouteResult(LatLonPoint startPoint,LatLonPoint endPoint) { final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint); DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode, null, null, "");// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路 routeSearch.calculateDriveRouteAsyn(query); } @Override public void onDriveRouteSearched(DriveRouteResult arg0, int arg1) { System.out.println("----------------"+arg1); Toast.makeText(this, "---"+arg1, Toast.LENGTH_SHORT).show(); if (arg1 == 0) { if (arg0 != null && arg0.getPaths() != null && arg0.getPaths().size() > 0) { driveRouteResult = arg0; DrivePath drivePath = driveRouteResult.getPaths().get(0); aMap.clear();// 清理地图上的所有覆盖物 DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay( this, aMap, drivePath, driveRouteResult.getStartPos(), driveRouteResult.getTargetPos()); drivingRouteOverlay.removeFromMap(); drivingRouteOverlay.addToMap(); drivingRouteOverlay.zoomToSpan(); } else { // ToastUtil.show(RouteActivity.this, R.string.no_result); Toast.makeText(this, "错误1", Toast.LENGTH_SHORT).show(); } } else if (arg1 == 27) { // ToastUtil.show(RouteActivity.this, R.string.error_network); Toast.makeText(this, "错误2", Toast.LENGTH_SHORT).show(); } else if (arg1 == 32) { // ToastUtil.show(RouteActivity.this, R.string.error_key); Toast.makeText(this, "错误3", Toast.LENGTH_SHORT).show(); } else { // ToastUtil.show(RouteActivity.this, getString(R.string.error_other) // + arg1); Toast.makeText(this, "错误4", Toast.LENGTH_SHORT).show(); } } @Override public void onBusRouteSearched(BusRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onWalkRouteSearched(WalkRouteResult arg0, int arg1) { // TODO Auto-generated method stub }
}
解决方案
http://www.cnblogs.com/ouyangduoduo/p/4623680.html