传入两个中文地点,解析成坐标点,并且在地图上显示出它们之间的路线图

来源:互联网 发布:php简单后台html5源码 编辑:程序博客网 时间:2024/06/06 02:23
package com.liukun.order_manager;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import com.alibaba.fastjson.JSONObject;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
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.WalkRouteResult;
import com.liukun.entity.OrderDetail;
import com.liukun.trailer.R;
import com.liukun.utils.AMapUtil;
import com.liukun.utils.BaseCacheCallBack;
import com.liukun.utils.Constants;
import com.liukun.utils.ToastUtil;
import com.liukun.view.TopBar;


import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


public class TrafficHelpDetailActivity extends AppCompatActivity implements GeocodeSearch.OnGeocodeSearchListener,
        AMapLocationListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter,
        RouteSearch.OnRouteSearchListener,AMap.OnMapClickListener ,AMap.OnMarkerClickListener{
    @ViewInject(R.id.topBar)
    private TopBar topBar;
    @ViewInject(R.id.traffic_help_detail_map)
    private MapView mapView;
    @ViewInject(R.id.traffic_help_detail_since)
    private TextView since;//起点
    @ViewInject(R.id.traffic_help_detail_end)
    private TextView end;//终点
    @ViewInject(R.id.traffic_help_detail_price)
    private TextView price;//价格
    @ViewInject(R.id.traffic_help_detail_time)
    private TextView time;//时间
    @ViewInject(R.id.traffic_help_detail_state)
    private TextView state;//订单状态
    @ViewInject(R.id.traffic_help_detail_number)
    private TextView number;//订单编号
    @ViewInject(R.id.traffic_help_detail_spend)
    private TextView traffic_help_detail_spend;
    private Marker geoMarker;
    private Marker regeoMarker;
    private AMap aMap;
    private GeocodeSearch geocoderSearch;
    private LatLonPoint mStartPoint ;//起点
    private LatLonPoint mEndPoint ;//终点
    private LatLonPoint midPoint;
    private DriveRouteResult mDriveRouteResult;
    private Context mContext;
    int i = 0;
    int j =0;
    private final int ROUTE_TYPE_DRIVE = 2;
    private RouteSearch mRouteSearch;
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private String code;
    double ss;
    double sss;
    private String orderNumber;//订单号
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x00:
//                    getPoint();
//                    setfromandtoMarker();
                    getLatlon(since.getText() + "");
                    break;
                case 0x01:
                    mStartPoint = new LatLonPoint(ss,sss);
                    getLatlon(end.getText()+"");
                    break;
                case 0x02:
                    mEndPoint = new LatLonPoint(ss,sss);
                    setfromandtoMarker();
                    searchRouteResult(ROUTE_TYPE_DRIVE, RouteSearch.DrivingDefault);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic_help_detail);
        x.view().inject(this);
        //title Bar
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        topBar.setmTitleView("订单详情");
        topBar.setOnTopBarClickListener(new TopBar.TopBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }


            @Override
            public void rightClick() {


            }
        });
        initData();
        downDoad();
        initListener();
    }
    private void set(LatLonPoint latLonPoint){
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(latLonPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.toend)));
    }
    private void setfromandtoMarker() {
//        Log.i("1234",mStartPoint.toString());
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mStartPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.begin)));
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mEndPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.toend)));
    }
    /**
     * 注册监听
     */
    private void initListener() {
        aMap.setOnMapClickListener(this);
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);
        aMap.setInfoWindowAdapter(this);
    }
    /** 下载数据 */
    private void downDoad(){
        RequestParams params = new RequestParams(Constants.ORDER_DETAIL);
        params.addBodyParameter("ordernumber",orderNumber);
        x.http().post(params, new BaseCacheCallBack() {
            @Override
            public void onFinished() {
                OrderDetail detail = JSONObject.parseObject(result,OrderDetail.class);
                since.setText(detail.getStartaddress());
                end.setText(detail.getEndaddress());
                state.setText(detail.getStatus());
                time.setText(detail.getOrderdate());
                price.setText(detail.getBridgeprice());
            }
        });
    }
    private void initData() {
        Intent intent = getIntent();
        orderNumber = intent.getStringExtra("orderNumber");
        number.setText(orderNumber);
        if (aMap == null) {
            aMap = mapView.getMap();
            geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
        }
        initListener();
        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);
        //高德地图初始化
        locationClient = new AMapLocationClient(this.getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为低功耗模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        // 设置定位监听
        locationClient.setLocationListener(this);
        //设置为单次定位
        locationOption.setOnceLocation(false);
        locationOption.setInterval(Long.valueOf(1000));
        initOption();
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(this);
//        getPoint();
    }
    // 根据控件的选择,重新设置定位参数
    private void initOption() {
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        // 设置定位参数
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
    }
    //关闭定位
    private void stopOption(){
        //停止定位
        locationClient.stopLocation();
    }
    /**
     * 响应地理编码
     */
    public void getLatlon(final String name) {
        GeocodeQuery query = new GeocodeQuery(name, code);// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,
        geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
    }
    /**
     * 地理编码查询回调
     */
    @Override
    public void onGeocodeSearched(GeocodeResult result, int rCode) {
        if (rCode == 1000) {
            if (result != null && result.getGeocodeAddressList() != null
                    && result.getGeocodeAddressList().size() > 0) {
                GeocodeAddress address = result.getGeocodeAddressList().get(0);
                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        AMapUtil.convertToLatLng(address.getLatLonPoint()), 20));
                geoMarker.setPosition(AMapUtil.convertToLatLng(address
                        .getLatLonPoint()));
//                addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:"
//                        + address.getFormatAddress();
                ss = address.getLatLonPoint().getLatitude();
                sss = address.getLatLonPoint().getLongitude();
//                midPoint = new LatLonPoint(address.getLatLonPoint().getLatitude(),address.getLatLonPoint().getLongitude());
//                Log.i("12314", midPoint.toString());
                if (i==0){
                    handler.sendEmptyMessage(0x01);
                }else{
                    handler.sendEmptyMessage(0x02);
                }
                i++;
            }
        }
    }
    public void getPoint(){
        Log.i("12314", since.getText()+"");
        getLatlon(since.getText() + "");
        mStartPoint = new LatLonPoint(ss,sss);
        getLatlon(end.getText()+"");
        mEndPoint = new LatLonPoint(ss,sss);
    }


    /**
     * 开始搜索路径规划方案
     */
    public void searchRouteResult(int routeType, int mode) {
//        if (mStartPoint == null) {
//            return;
//        }
//        if (mEndPoint == null) {
//            return;
//        }
        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                mStartPoint, mEndPoint);
         if (routeType == ROUTE_TYPE_DRIVE) {// 驾车路径规划
            RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, mode, null,
                    null, "");// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
            mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
        }
    }
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {


    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (null != locationClient) {
            stopOption();
            /**
             * 如果AMapLocationClient是在当前Activity实例化的,
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (null != aMapLocation&&j==0) {
            code = aMapLocation.getCityCode();
            handler.sendEmptyMessage(0x00);
        }else{
            midPoint = new LatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());
            set(midPoint);
        }
        j++;
    }


    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }


    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }


    @Override
    public void onInfoWindowClick(Marker marker) {


    }


    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }


    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {


    }


    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == 1000) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mDriveRouteResult = result;
                    final DrivePath drivePath = mDriveRouteResult.getPaths()
                            .get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            this, aMap, drivePath,
                            mDriveRouteResult.getStartPos(),
                            mDriveRouteResult.getTargetPos());
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                    int dis = (int) drivePath.getDistance();
                    int dur = (int) drivePath.getDuration();
//                    String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";///////////
//                    int taxiCost = (int) mDriveRouteResult.getTaxiCost();
                    traffic_help_detail_spend.setText("用时"+AMapUtil.getFriendlyTime(dur)+" "+"行驶"+AMapUtil.getFriendlyLength(dis));
                }
            }
        } else {
            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }




    }


    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {


    }


    @Override
    public void onMapClick(LatLng latLng) {


    }
}
0 0
原创粉丝点击