采用高德地图 实现打车功能代码

1:移动视角 
要用:aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
 
  
aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));显示的范围不准确
//    根据动画按钮的状态来选择不同的camera来改变可视区域
//    AMap.CanclelableCallback:当一个任务完成或关闭时的回调接口。
     private void changeCamera(CameraUpdate update, AMap.CancelableCallback callback){
         boolean animated = ((CompoundButton)findViewById(R.id.animate)).isChecked();
         if (animated){
//在指定的持续时间内,动画地移动地图到指定的位置,完成时调用可选的回调方法。 如果运动过程中调用getCameraPosition(),它将返回可视区域飞行中的当前位置。
//            第一个参数:定义转换的时间,第二个参数:运动的持续时间,第三个参数:回调函数
             aMap.animateCamera(update, 1000 ,callback);
         } else {
//            照传入的CameraUpdate 参数移动可视区域。这个方法为瞬间移动,没有移动过程,如果在调用此方法后再调用getCameraPosition()将返回移动后位置。
             aMap.moveCamera(update);
         }
     }
 
2:构建矩形可见区域
 
  
/**
 * 构建矩形可见区域
 *
 * @return
 */
private LatLngBounds getFELatLngBounds() {
    LatLngBounds.Builder b = LatLngBounds.builder();
    for (int i = 0; i < mLatLngs.size(); i++) {
        b.include(new LatLng(mLatLngs.get(i).latitude,
                mLatLngs.get(i).longitude));
    }

    b.include(endLatlng).include(nowLatlng).include(startLatlng);
    return b.build();
}
3:跳转高德地图
private void openGaoDeMap() {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.setData(Uri.parse("androidamap://route?sourceApplication="
            + getPackageName()
            + "&slat=" + tripInfo.getFrom_lat() + "&slon=" + tripInfo.getFrom_lng()
            + "&sname=" + tripInfo.getFrom_address() + "&dlat=" + tripInfo.getTo_lat()
            + "&dlon=" + tripInfo.getTo_lng() + "&dname=" + tripInfo.getTo_address()
            + "&dev=1&m=0&t=0&showType=0&&style=2"));

    if (isInstallByread("com.autonavi.minimap")) {
        startActivity(intent); //启动调用
    } else {
        showTextDialog("没有安装高德地图客户端");
    }
}
private boolean isInstallByread(String packageName) {
    return new File("/data/data/" + packageName).exists();
}
4:平滑移动小车
 
  
 private void movie() {
        mLatLngmovies.clear();
        mLatLngmovies.add(BaseApplication.getInstance().getLatLngs()
                .get(BaseApplication.getInstance().getLatLngs().size() - 3));
        mLatLngmovies.add(BaseApplication.getInstance().getLatLngs()
                .get(BaseApplication.getInstance().getLatLngs().size() - 2));
        mLatLngmovies.add(BaseApplication.getInstance().getLatLngs()
                .get(BaseApplication.getInstance().getLatLngs().size() - 1));
//        if (polyine == null) {
//            ToastUtil.showShortToast(mContext, "请先规划好路线");
//            return;
//        }
        if (marker != null)
            marker.destroy();

//        if (!istach) {
        // 移动map的镜头
        LatLngBounds bounds = getLatLngBounds();
        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
//        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
//        }

        LatLng drivePoint = mLatLngmovies.get(0);
        Pair pair = SpatialRelationUtil.calShortestDistancePoint(mLatLngmovies, drivePoint);
        mLatLngmovies.set(pair.first, drivePoint);
        List subList = mLatLngmovies.subList(pair.first, mLatLngmovies.size());
// 设置滑动的轨迹左边点
        smoothMarker.setPoints(subList);
// 设置滑动的总时间
        smoothMarker.setTotalDuration(time);
// 开始滑动
        smoothMarker.startSmoothMove();
    }
 
  
5:轨迹纠偏
 
  
/**
 * 调起一次轨迹纠偏
 */
private void traceGrasp() {
    if (mOverlayList.containsKey(mSequenceLineID)) {
        cleanFinishTrace();
        TraceOverlay overlay = new TraceOverlay(aMap);
        mOverlayList.put(mSequenceLineID, overlay);
        List mapList = traceLocationToMap(mTraceList);
        if (arrive1) {
            mapList.add(startLatlng);
            mapList.add(endLatlng);
            mapList.add(nowLatlng);
            // 移动地图,所有marker自适应显示。LatLngBounds与地图边缘10像素的填充区域
            overlay.setProperCamera(mapList);
        }
        mTraceClient.queryProcessedTrace(mSequenceLineID, mTraceList,
                mCoordinateType, this);
        int status = overlay.getTraceStatus();
        String tipString = "";
        if (status == TraceOverlay.TRACE_STATUS_PROCESSING) {
            tipString = "该线路轨迹纠偏进行中...";
            setDistanceWaitInfo(overlay);
        } else if (status == TraceOverlay.TRACE_STATUS_FINISH) {
            setDistanceWaitInfo(overlay);
            tipString = "该线路轨迹已完成";
        } else if (status == TraceOverlay.TRACE_STATUS_FAILURE) {
            tipString = "该线路轨迹失败";
        } else if (status == TraceOverlay.TRACE_STATUS_PREPARE) {
            tipString = "该线路轨迹纠偏已经开始";
        }
        Toast.makeText(this.getApplicationContext(), tipString,
                Toast.LENGTH_SHORT).show();
        return;
    }
    TraceOverlay mTraceOverlay = new TraceOverlay(aMap);
    mOverlayList.put(mSequenceLineID, mTraceOverlay);
    List mapList = traceLocationToMap(mTraceList);
    if (arrive1) {
        mapList.add(startLatlng);
        mapList.add(endLatlng);
        mapList.add(nowLatlng);
        mTraceOverlay.setProperCamera(mapList);
    }
    mResultShow.setText(mDistanceString);
    mLowSpeedShow.setText(mStopTimeString);
    mTraceClient.queryProcessedTrace(mSequenceLineID, mTraceList,
            mCoordinateType, this);
}
/**
 * 轨迹纠偏点转换为地图LatLng
 *
 * @param traceLocationList
 * @return
 */
public List traceLocationToMap(List traceLocationList) {
    List mapList = new ArrayList();
    for (TraceLocation location : traceLocationList) {
        LatLng latlng = new LatLng(location.getLatitude(),
                location.getLongitude());
        mapList.add(latlng);
    }
    return mapList;
}

你可能感兴趣的:(安卓笔记)