高德升级版本

- 编辑:小昭 -

高德升级版本


代码
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

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.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
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.maps.model.MyLocationStyle;
import com.amap.api.services.core.AMapException;
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.RidePath;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.zxwl.gddd.overlay.DrivingRouteOverlay;
import com.zxwl.gddd.overlay.RideRouteOverlay;
import com.zxwl.gddd.overlay.WalkRouteOverlay;
import com.zxwl.gddd.util.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity2 extends AppCompatActivity implements LocationSource, AMapLocationListener, AMap.OnMarkerClickListener, RouteSearch.OnRouteSearchListener {

    @Bind(R.id.radio1)
    RadioButton radio1;
    @Bind(R.id.radioButton2)
    RadioButton radioButton2;
    @Bind(R.id.radioButton3)
    RadioButton radioButton3;
    @Bind(R.id.largeLabel)
    LinearLayout largeLabel;
    @Bind(R.id.map)
    MapView aMapView;
    @Bind(R.id.rg)
    RadioGroup rg;
    private OnLocationChangedListener listener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    private RouteSearch search;
    private RouteSearch.DriveRouteQuery query;
    private RouteSearch.FromAndTo fromAndTo;
    private DriveRouteResult mDriveRouteResult;
    private RouteSearch.WalkRouteQuery walkRouteQuery;
    private RouteSearch.RideRouteQuery rideRouteQuery;
    private BusRouteResult mBusRouteResult;
    private RouteSearch.BusRouteQuery busRouteQuery;
    private WalkRouteResult mWalkRouteResult;
    private RideRouteResult mRideRouteResult;
    private AMap aMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        aMap = aMapView.getMap();
        aMapView.onCreate(savedInstanceState);


    }

    @Override
    protected void onResume() {
        super.onResume();
        aMapView.onResume();

        if (Build.VERSION.SDK_INT > 23) {
            MyLocationStyle myLocationStyle;
            myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
            myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
            aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
            aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。
            aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
            myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
            myLocationStyle.showMyLocation(true);
        }


        try {
            if (Build.VERSION.SDK_INT <= 22) {
                // 设置定位监听
                aMap.setLocationSource(this);
                // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
                aMap.setMyLocationEnabled(true);
                // 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种
                aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
                mlocationClient.startLocation();//启动定位
            }
        } catch (Exception e) {
            Log.e("TAG", e.toString());
        }

        LatLng latLng = new LatLng(22.855985, 113.082562);
        aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));

        aMap.setOnMarkerClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        aMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        aMapView.onDestroy();
        if (null != mlocationClient) {
            mlocationClient.onDestroy();
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        aMapView.onSaveInstanceState(outState);
    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        this.listener = listener;
        if (mlocationClient == null) {
            //初始化定位
            mlocationClient = new AMapLocationClient(this);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位回调监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
            // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后,在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
            mlocationClient.startLocation();//启动定位
        }
    }

    @Override
    public void deactivate() {
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (listener != null && amapLocation != null) {
            if (amapLocation != null
                    && amapLocation.getErrorCode() == 0) {
                listener.onLocationChanged(amapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {

        LatLng end = marker.getPosition();
        Location start = aMap.getMyLocation();


        fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(start.getLatitude(), start.getLongitude()), new LatLonPoint(end.latitude, end.longitude));
        search = new RouteSearch(this);
        search.setRouteSearchListener(this);

        switch (rg.getCheckedRadioButtonId()) {
            case R.id.radio1:
                query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_MULTI_CHOICE_AVOID_CONGESTION_NO_HIGHWAY_SAVE_MONEY, null, null, "");
                search.calculateDriveRouteAsyn(query);
                break;
            case R.id.radioButton2:
                rideRouteQuery = new RouteSearch.RideRouteQuery(fromAndTo);
                search.calculateRideRouteAsyn(rideRouteQuery);
                break;
            case R.id.radioButton3:
                walkRouteQuery = new RouteSearch.WalkRouteQuery(fromAndTo);
                search.calculateWalkRouteAsyn(walkRouteQuery);
                break;
        }
        return true;
    }

    @Override
    public void onBusRouteSearched(BusRouteResult result, int errorCode) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mDriveRouteResult = result;
                    final DrivePath drivePath = mDriveRouteResult.getPaths()
                            .get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            MainActivity2.this, aMap, drivePath,
                            mDriveRouteResult.getStartPos(),
                            mDriveRouteResult.getTargetPos(), null);
                    drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
                    drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                } else if (result != null && result.getPaths() == null) {
                    ToastUtil.show(this, R.string.no_result);
                }
            } else {
                ToastUtil.show(this, R.string.no_result);
            }
        } else {
            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mWalkRouteResult = result;
                    final WalkPath walkPath = mWalkRouteResult.getPaths()
                            .get(0);
                    WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
                            this, aMap, walkPath,
                            mWalkRouteResult.getStartPos(),
                            mWalkRouteResult.getTargetPos());
                    walkRouteOverlay.removeFromMap();
                    walkRouteOverlay.addToMap();
                    walkRouteOverlay.zoomToSpan();
                } else if (result != null && result.getPaths() == null) {
                    ToastUtil.show(this, R.string.no_result);
                }
            } else {
                ToastUtil.show(this, R.string.no_result);
            }
        } else {
            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mRideRouteResult = result;
                    final RidePath ridePath = mRideRouteResult.getPaths()
                            .get(0);
                    RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
                            this, aMap, ridePath,
                            mRideRouteResult.getStartPos(),
                            mRideRouteResult.getTargetPos());
                    rideRouteOverlay.removeFromMap();
                    rideRouteOverlay.addToMap();
                    rideRouteOverlay.zoomToSpan();
                } else if (result != null && result.getPaths() == null) {
                    ToastUtil.show(this, R.string.no_result);
                }
            } else {
                ToastUtil.show(this, R.string.no_result);
            }
        } else {
            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }
}



布局



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zxwl.gddd.MainActivity2">

    <LinearLayout
        android:id="@+id/largeLabel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

<RadioGroup
    android:id="@+id/rg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioButton
        android:checked="true"
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_weight="0.04"
        android:text="汽车" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.04"
        android:text="自行车" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.04"
        android:text="步行" />

</RadioGroup>

    </LinearLayout>

    <com.amap.api.maps.MapView

        android:id="@+id/map"

        android:layout_width="match_parent"

        android:layout_height="match_parent"
        android:layout_below="@+id/largeLabel">

    </com.amap.api.maps.MapView>
</RelativeLayout>