自定义内容:起点坐标,终点坐标,路线颜色
第一步
添加权限
<!--允许程序打开网络套接字-->
<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" />
第二步
添加开发者key
<meta-data android:name="com.amap.api.v2.apikey"
android:value="你的key">//开发者申请的key
第三步
添加jar包
一个高德搜索包,一个3D地图包
下载地址http://lbs.amap.com/api/android-sdk/download/
AMap_Search_V3.6.0_20161111.jar
Android_Map3D_SDK_V4.1.2_20161104.jar
第三步
在布局文件添加地图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.boatt.gaodedemo.MainActivity">
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
第四步
自定义路线图
public class NewDrivingRouteOverlay extends DrivingRouteOverlay {
Context mContext;
public NewDrivingRouteOverlay(Context arg0, AMap arg1, DrivePath arg2,
LatLonPoint arg3, LatLonPoint arg4) {
super(arg0, arg1, arg2, arg3, arg4);
mContext = arg0;
}
//自定义结束位置marker
@Override
protected BitmapDescriptor getEndBitmapDescriptor() {
return BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(mContext.getResources(), R.mipmap.start));//此处添加你的图标
}
//自定义开始位置marker
@Override
protected BitmapDescriptor getStartBitmapDescriptor() {
return BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(mContext.getResources(), R.mipmap.end));//此处添加你的图标
}
//自定义路线颜色
@Override
protected int getDriveColor() {
return Color.BLACK;
}
}
第五步
开始规划路线图
public class MainActivity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener {
private MapView mMapView;
private RouteSearch routeSearch;
private AMap aMap;
private static final String TAG = "MainActivity";
private LatLonPoint latLonPoint1;
private LatLonPoint latLonPoint2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.map);
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
mMapView.onCreate(savedInstanceState);
aMap = mMapView.getMap();
routeSearch = new RouteSearch(this);
routeSearch.setRouteSearchListener(this);
latLonPoint1 = new LatLonPoint(31.075746, 121.503216);
latLonPoint2 = new LatLonPoint(31.080069, 121.524986);
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(
new RouteSearch.FromAndTo(latLonPoint1,
latLonPoint2), RouteSearch.DrivingDefault, null, null, "");
routeSearch.calculateDriveRouteAsyn(query);
Log.d(TAG, "onCreate: "+1);
aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(
new LatLng(31.075746,121.503216),new LatLng(31.080069, 121.524986)),20));
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
Log.d(TAG, "onDestroy: "+4);
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
mMapView.onResume();
Log.d(TAG, "onResume: "+2);
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理
mMapView.onSaveInstanceState(outState);
}
@Override
public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
}
@Override
public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
Log.d(TAG, "onDriveRouteSearched: "+3);
DrivePath drivePath = driveRouteResult.getPaths().get(0);
NewDrivingRouteOverlay drivingRouteOverlay = new NewDrivingRouteOverlay(this, aMap, drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());
drivingRouteOverlay.setNodeIconVisibility(false);//隐藏转弯的节点
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(
new LatLng(latLonPoint1.getLatitude(),latLonPoint1.getLongitude()),
new LatLng(latLonPoint2.getLatitude(),latLonPoint2.getLongitude())),50));
}
@Override
public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
}
@Override
public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
}
}
最后补充
drivingRouteOverlay.setNodeIconVisibility(false);//隐藏转弯的节点
drivingRouteOverlay.addToMap();//把路线图添加到地图
drivingRouteOverlay.zoomToSpan();//在地图上显示路线图
aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds( new LatLng(latLonPoint1.getLatitude(),latLonPoint1.getLongitude()), new LatLng(latLonPoint2.getLatitude(),latLonPoint2.getLongitude())),50));}
//动态缩放地图大小,LatLngBounds:地图要显示的矩形大小,50:距离屏幕四周的padding值