android 高德地图 驾车路线规划

来源:互联网 发布:天天直播网络东方卫视 编辑:程序博客网 时间:2024/04/28 08:44

  

在此贴出高德地图驾车路线规划(导航) 其它引用到的类请自行到官方下载演示demo进行配置 ,希望能帮到有需要的人.

            -----------------------------------------------无敌分割线------------------------------------------------

import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import android.widget.RelativeLayout;import com.amap.api.maps.AMap;import com.amap.api.maps.CameraUpdate;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.UiSettings;import com.amap.api.maps.model.BitmapDescriptorFactory;import com.amap.api.maps.model.CameraPosition;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.Marker;import com.amap.api.maps.model.MarkerOptions;import com.amap.api.navi.AMapNavi;import com.amap.api.navi.AMapNaviException;import com.amap.api.navi.AMapNaviListener;import com.amap.api.navi.AMapNaviView;import com.amap.api.navi.AMapNaviViewOptions;import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;import com.amap.api.navi.model.NaviLatLng;import com.amap.api.navi.view.RouteOverLay;import com.amap.api.navi.view.ZoomButtonView;import java.util.ArrayList;import java.util.List;public class CustomRouteActivity extends BaseActivity implements AMapNaviListener {    private ArrayList<NaviLatLng> mWayPointList;    protected final List<NaviLatLng> sList = new ArrayList<NaviLatLng>();    protected final List<NaviLatLng> eList = new ArrayList<NaviLatLng>();    private double latitudeStart, longitudeStart, marketLatitude,            marketLongitude, latitudeEnd, longitudeEnd;    private NaviLatLng wayPoint;    private ImageButton ibBack;    private ZoomButtonView mZoomButtonView;    private AMap aMap;    private ImageButton ibRefresh;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Bundle bundle = getIntent().getExtras();
        //经纬度       if (null != bundle) {            latitudeStart = bundle.getDouble("latitude");            longitudeStart = bundle.getDouble("longitude");            marketLatitude = bundle.getDouble("marketLatitude");            marketLongitude = bundle.getDouble("marketLongitude");            latitudeEnd = bundle.getDouble("latitudeEnd");            longitudeEnd = bundle.getDouble("longitudeEnd");        }        NaviLatLng mStartLatLng = new NaviLatLng(latitudeStart, longitudeStart);        NaviLatLng mEndLatLng = new NaviLatLng(latitudeEnd, longitudeEnd);        wayPoint = new NaviLatLng(marketLatitude, marketLongitude);        sList.add(mStartLatLng);        eList.add(mEndLatLng);        mWayPointList = new ArrayList<NaviLatLng>();        setContentView(R.layout.activity_basic_navi);        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);        mAMapNaviView.onCreate(savedInstanceState);        mAMapNaviView.setAMapNaviViewListener(this);        ibBack = (ImageButton) findViewById(R.id.ib_back);        //自定义缩放控件        mZoomButtonView = (ZoomButtonView) findViewById(R.id.myZoomButtonView);        aMap = mAMapNaviView.getMap();        mAMapNaviView.setLazyZoomButtonView(mZoomButtonView);        //移动到当前位置        UiSettings mUiSettings = aMap.getUiSettings();        mUiSettings.setZoomControlsEnabled(false);        //指南针是否显示        mUiSettings.setCompassEnabled(false);        //15是缩放比例,0是倾斜度,30显示比例        CameraUpdate mUpdata = CameraUpdateFactory.newCameraPosition(        new CameraPosition(new LatLng(latitudeStart, longitudeStart), 14, 0, 30));//这是地理位置,就是经纬度。        aMap.moveCamera(mUpdata);//定位的方法       //绘制蓝点
        drawMarkers();        AMapNaviViewOptions options = mAMapNaviView.getViewOptions();        options.setLayoutVisible(false);        //主动隐藏导航光柱        options.setTrafficBarEnabled(false);        //关闭自动绘制路线(如果你想自行绘制路线的话,必须关闭!!!)        options.setAutoDrawRoute(false);        //关闭自动绘制路线(如果你想自行绘制路线的话,必须关闭!!!)        options.setAutoDrawRoute(false);        mAMapNaviView.setViewOptions(options);        mWayPointList.add(wayPoint);        }        public void drawMarkers() {            Marker marker = aMap.addMarker(new MarkerOptions()                    .position(new LatLng(latitudeStart,longitudeStart))                    .title("我的位置")                    .icon(BitmapDescriptorFactory                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))                    .draggable(true));//                     marker.showInfoWindow();// 设置默认显示一个infowinfow        }    @Override    public void onInitNaviSuccess() {        super.onInitNaviSuccess();        /**         * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multiplerout         * @congestion 躲避拥堵         * @avoidhightspeed 不走高速         * @cost 避免收费         * @hightspeed 高速优先         * @multipleroute 多路         *  说明: 以上参数都是boolean类型,其中multipleroute参数表示是否多条路线,如果为true则此策略会算出多条路线。         *  注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true         */        int strategy = 0;        try {            //再次强调,最后一个参数为true时代表多路径,否则代表单路径            strategy = mAMapNavi.strategyConvert(true, false, false, false, false);        } catch (Exception e) {            e.printStackTrace();        }        mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy);    }    @Override    protected void onResume() {        super.onResume();    }    @Override    public void onCalculateRouteSuccess(int[] ids) {//        如果根据获取的导航路线来自定义绘制        RouteOverLay routeOverlay = new RouteOverLay(mAMapNaviView.getMap(), mAMapNavi.getNaviPath(), this);        routeOverlay.setStartPointBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.map_user_ic));        routeOverlay.setEndPointBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.map_business_ic));        routeOverlay.setWayPointBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.map_dispatching_ic));        routeOverlay.setTrafficLine(false);        try {            routeOverlay.setWidth(30);        } catch (AMapNaviException e) {            //宽度须>0            e.printStackTrace();        }        int color[] = new int[10];        color[0] = getResources().getColor(R.color.color_FF4E80);        color[1] = getResources().getColor(R.color.color_FF4E80);        color[2] = Color.BLUE;        color[3] = Color.YELLOW;        color[4] = Color.GRAY;        routeOverlay.addToMap(color, mAMapNavi.getNaviPath().getWayPointIndex());        mAMapNavi.startNavi(AMapNavi.GPSNaviMode);    }    @Override    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {    }}
原创粉丝点击