高德地图之自定义路线图

自定义内容:起点坐标,终点坐标,路线颜色

Paste_Image.png
Paste_Image.png

第一步

添加权限













第二步

添加开发者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

第三步

在布局文件添加地图


 



第四步

自定义路线图

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值

你可能感兴趣的:(高德地图之自定义路线图)