1. 程式人生 > >採用高德地圖 實現打車功能程式碼

採用高德地圖 實現打車功能程式碼

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<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(mLatLngmovies, drivePoint);
        mLatLngmovies.set(pair.first, drivePoint);
        List<LatLng> 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<LatLng> 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<LatLng> 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<LatLng> traceLocationToMap(List<TraceLocation> traceLocationList) {
    List<LatLng> mapList = new ArrayList<LatLng>();
    for (TraceLocation location : traceLocationList) {
        LatLng latlng = new LatLng(location.getLatitude(),
                location.getLongitude());
        mapList.add(latlng);
    }
    return mapList;
}