oto餐飲app實時訂單物流追蹤系統架構設計(一)

1.引言
.1 1.1目的
不管是淘點點或者餓了麼,只要是打算做外賣的app,我覺得沒有實時地圖展示外賣訂單位置信息是說不過去的,因爲我覺得外賣對於物流實時性是非常苛刻的。11:00定的外賣在12:00還沒有送到用戶手中,都是不可原諒的。
基於此我想設計一個地圖實時顯示送貨員位置信息的系統。
2.1 功能需求
該系統主要功能包括:
1).配送員app登錄上線後,接收訂單後循環的把自己的位置信息發送給服務器管理
2).用戶客戶端在訂單經過店家轉給配送員後,即可在地圖上查看實時位置信息
3)服務器收到配送員位置信息後根據配送訂單信息轉發給相應的用戶客戶端
4)其他功能。用戶app提交提單給web服務器後,web服務器把這條消息推送給訂單追蹤服務器
3.1 業務流程設計
這裏寫圖片描述4.1界面設計
1)店家版
這裏寫圖片描述 2)配送員這裏寫圖片描述這裏寫圖片描述5)各個模塊詳細設計和實現
5.1 用戶app請求地理位置`public class MyUdpClient extends UDPClientBase {

    public MyUdpClient(byte[] uuid, int appid, String serverAddr, int serverPort)  
            throws Exception {  
        super(uuid, appid, serverAddr, serverPort);  

    }  

    @Override  
    public boolean hasNetworkConnection() {  
        return Util.hasNetwork(OnlineService.this);  
    }  


    @Override  
    public void trySystemSleep() {  
        tryReleaseWakeLock();  
    }  

    @Override  
    public void onPushMessage(Message message) {  
        if(message == null){  
            return;  
        }  
        else if(message.getData() == null || message.getData().length == 0){  
            return;  
        }  
        else if(message.getCmd() == 16){// 0x10 通用推送信息  

        }  
        else if(message.getCmd() == 17){// 0x11 分組推送信息  

        }  
        else if(message.getCmd() == 32){// 0x20 自定義推送信息  
            String str = null;  
            try{  
                str = new String(message.getData(),5,message.getContentLength(), "UTF-8");  
            }catch(Exception e){  
                str = Util.convert(message.getData(),5,message.getContentLength());  
            }  

            //轉化成json串   lbs 實時物流追中,  
            Gson gson = new Gson();  
            MsgType msgType = gson.fromJson(str,new TypeToken<MsgType>(){}.getType());   
            if(msgType.equals("wuliu"))  
            {  
                //配送員地址位置  

            }  
            else if(msgType.equals("order"))  
            {  
                //訂單確認  
            }  
            else  
            {  
                notifyUser(32,"自定義推送信息",""+str,"收到自定義推送信息");  
            }         
        }  
        setPkgsInfo();  
    }  

}  `

百度地圖api版本v_3_1,百度定位api版本v_4.2
package com.cmyy.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.cmyy.R;
import com.cmyy.push.OnlineService;
import com.cmyy.push.Params;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

//實時物流頁面
public class LogisticsTrackingActivity extends BaseActivity{
private static final String LTAG = LogisticsTrackingActivity.class.getSimpleName();
private MapView mMapView;
private BaiduMap mBaiduMap;
private SDKReceiver mReceiver;
// 定位相關
private LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner(); //定位
boolean isFirstLoc = true;// 是否首次定位

private PushLocationReceiver mPushLocationReceice;  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    // 註冊 SDK 廣播監聽者  
    IntentFilter iFilter = new IntentFilter();  
    iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);  
    iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);  
    mReceiver = new SDKReceiver();  
    registerReceiver(mReceiver, iFilter);  

    setContentView(R.layout.activity_tracking);  

    //向服務器通知需要物流地理位置了  

    //註冊接受位置信息廣播  
    IntentFilter ipushFilter = new IntentFilter();  
    ipushFilter.addAction("com.cmyy.ui.LogisticsTrackingActivity.PushLocationReceiver");  
    mPushLocationReceice = new PushLocationReceiver();  
    registerReceiver(mPushLocationReceice, ipushFilter);  

    //啓動服務去接收位置   
    saveAccountInfo();  
    Intent startSrv = new Intent(this, OnlineService.class);  
    startSrv.putExtra("CMD", "RESET");  
    startService(startSrv);  
    //展示地圖  
    mMapView = (MapView) findViewById(R.id.bmapView);  
    mBaiduMap = mMapView.getMap();  
    // 開啓定位圖層  
    mBaiduMap.setMyLocationEnabled(true);  
    // 定位初始化  
    /*mLocClient = new LocationClient(this); 
    mLocClient.registerLocationListener(myListener); 
    LocationClientOption option = new LocationClientOption(); 
    option.setOpenGps(true);// 打開gps 
    option.setCoorType("bd09ll"); // 設置座標類型 
    option.setScanSpan(1000); 
    mLocClient.setLocOption(option); 
    mLocClient.start();*/  
}  

public class PushLocationReceiver extends BroadcastReceiver {  

    public PushLocationReceiver() {  

    }  

    @Override  
    public void onReceive(Context context, Intent intent) {  
         //接收到配送員發送過來的位置信息  
        String message = intent.getStringExtra("data");  
        Log.i("receive",message);  
        Gson gson = new Gson();  
        MyLocationData locData = gson.fromJson(message,new TypeToken<MyLocationData>(){}.getType());  
        JSONObject jsonObj;  
        double latitude = 39.915;  
        double longitude = 116.404;  
        try {  
            jsonObj = new JSONObject(message);  
            latitude = jsonObj.getDouble("latitude");  
            longitude= jsonObj.getDouble("longitude");  
        } catch (JSONException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        Log.i("receive",String.valueOf(latitude));  
        Log.i("receive",String.valueOf(longitude));  
        mBaiduMap.setMyLocationData(locData);  
        if (isFirstLoc) {  
            isFirstLoc = false;  
            LatLng ll = new LatLng(latitude,  
                    longitude);  
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);  
            mBaiduMap.animateMapStatus(u);  
        }  
    }  

}  

/** 
 * 定位SDK監聽函數 
 */  
public class MyLocationListenner implements BDLocationListener {  

    @Override  
    public void onReceiveLocation(BDLocation location) {  
        // map view 銷燬後不在處理新接收的位置  
        if (location == null || mMapView == null)  
            return;  
        MyLocationData locData = new MyLocationData.Builder()  
                .accuracy(location.getRadius())  
                // 此處設置開發者獲取到的方向信息,順時針0-360  
                .direction(100).latitude(location.getLatitude())  
                .longitude(location.getLongitude()).build();  
        mBaiduMap.setMyLocationData(locData);  
        if (isFirstLoc) {  
            isFirstLoc = false;  
            LatLng ll = new LatLng(location.getLatitude(),  
                    location.getLongitude());  
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);  
            mBaiduMap.animateMapStatus(u);  
        }  
    }  

    public void onReceivePoi(BDLocation poiLocation) {  
    }  
}  

/** 
 * 構造廣播監聽類,監聽 SDK key 驗證以及網絡異常廣播 
 */  
public class SDKReceiver extends BroadcastReceiver {  
    public void onReceive(Context context, Intent intent) {  
        String s = intent.getAction();  
        Log.d(LTAG, "action: " + s);  
        if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {  
            Log.e("LogisticsTrackingActivity","key 驗證出錯! 請在 AndroidManifest.xml 文件中檢查 key 設置");  
        } else if (s.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {  
            Log.e("LogisticsTrackingActivity","網絡出錯");  
        }  
    }  
}  

protected void saveAccountInfo(){  
    SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME,Context.MODE_PRIVATE);  
    SharedPreferences.Editor editor = account.edit();  
    editor.putString(Params.SERVER_IP, "192.168.88.130");  
    editor.putString(Params.SERVER_PORT, "9966");  
    editor.putString(Params.PUSH_PORT, "9998");  
    editor.putString(Params.USER_NAME, "333");  
    editor.putString(Params.SENT_PKGS, "0");  
    editor.putString(Params.RECEIVE_PKGS, "0");  
    editor.commit();      
}  

@Override  
protected void onPause() {  
    super.onPause();  
    // activity 暫停時同時暫停地圖控件  
    mMapView.onPause();  
}  

@Override  
protected void onResume() {  
    super.onResume();  
    // activity 恢復時同時恢復地圖控件  
    mMapView.onResume();  
}  

@Override  
protected void onDestroy() {  
    mLocClient.stop();  
    // 關閉定位圖層  
    mBaiduMap.setMyLocationEnabled(false);  
    super.onDestroy();  
    // 取消監聽 SDK 廣播  
    unregisterReceiver(mReceiver);  
    // activity 銷燬時同時銷燬地圖控件  
    mMapView.onDestroy();  
}  

}

配送員app發送地理位置信息給追蹤服務器

//接受服務器推送信息  
   saveAccountInfo();   //設置服務器ip 端口  
   Intent startSrv = new Intent(this, OnlineService.class);  
startService(startSrv);  

   push = new Push(this);  
   //啓動地理位置監控  
   mBaiduMapControl = new BaiduMapControl();  
   mBaiduMapControl.Location(getApplicationContext(),new BDLocationListener()  
   {  
    @Override  
    public void onReceiveLocation(BDLocation location) {  
        MyLocationData locData = new MyLocationData.Builder()  
        .accuracy(location.getRadius())  
        // 此處設置開發者獲取到的方向信息,順時針0-360  
        .direction(100).latitude(location.getLatitude())  
        .longitude(location.getLongitude()).build();  
         Gson gson = new Gson();  
         //地理位置信息  
         String strLocation = gson.toJson(locData).toString();  
         Log.i("BaiduLocationApiDem",strLocation);  
        //Receive Location   
        /*StringBuffer sb = new StringBuffer(256); 
        sb.append("time : "); 
        sb.append(location.getTime());             //定位時間 
        sb.append("\nerror code : "); 
        sb.append(location.getLocType()); 
        sb.append("\nlatitude : "); 
        sb.append(location.getLatitude());       // 開發者獲取到的方向信息,順時針0-360 
        sb.append("\nlontitude : "); 
        sb.append(location.getLongitude()); 
        sb.append("\nradius : "); 
        sb.append(location.getRadius());             
        if (location.getLocType() == BDLocation.TypeGpsLocation){ 
            sb.append("\nspeed : "); 
            sb.append(location.getSpeed()); 
            sb.append("\nsatellite : "); 
            sb.append(location.getSatelliteNumber()); 
            sb.append("\ndirection : "); 
            sb.append("\naddr : "); 
            sb.append(location.getAddrStr()); 
            sb.append(location.getDirection()); 
        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ 
            sb.append("\naddr : "); 
            sb.append(location.getAddrStr()); 
            //運營商信息 
            sb.append("\noperationers : "); 
            sb.append(location.getOperators()); 
        } 

        Log.i("BaiduLocationApiDem", sb.toString());    */  
        //定位成功發送給服務器  
        push.send0x20("333", strLocation);    

    }  

   });    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章