1. 程式人生 > >Android基站定位——三基站(多基站)定位(三)

Android基站定位——三基站(多基站)定位(三)

闡述幾個概念:

基站:類似於WIFI熱點,官方解釋,移動通訊系統中,連線固定部分與無線部分,並通過空中的無線傳輸與移動臺相連的裝置。基站即公用行動通訊基站是無線電臺站的一種形式,是指在一定的無線電覆蓋區中,通過行動通訊交換中心,與行動電話終端之間進行資訊傳遞的無線電收發信電臺。

單基站定位是指:通過手機獲取當前連線到的基站資訊,來確定使用者的大概位置(使用者可能在某個基站(手機當前連線的基站)發出的無線電覆蓋區域中)。

三基站或多基站:通過手機獲取附近區域的基站資訊(不是使用者當前連線的基站,得到的是一組基站資訊),來確定使用者的大概位置。

獲取鄰區基站資訊

        // 獲取鄰區基站資訊
        List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();
        StringBuffer sb = new StringBuffer("總數 : " + infos.size() + "\n");
        for (NeighboringCellInfo info1 : infos) { // 根據鄰區總數進行迴圈
            sb.append(" LAC : " + info1.getLac()); // 取出當前鄰區的LAC
            sb.append(" CID : " + info1.getCid()); // 取出當前鄰區的CID
            sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 獲取鄰區基站訊號強度
        }


注:中國聯通的基本上獲取不到附近的基站,我測的時候使用的是中國移動。

一、探索:

       1、我猜想請求引數的Json是這樣組裝的:

        JSONObject json = new JSONObject();
        json.put("version", "1.1.0");
        json.put("host", "maps.google.com");
        json.put("location_area_code", "6338");
        json.put("mobile_country_code", "460");
        json.put("mobile_network_code", "0");
        json.put("age", 0);
        
        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("cell_id", "62291");
        json1.put("signal_strength", -95);
        array.put(json1);
        
        JSONObject json2 = new JSONObject();
        json2.put("cell_id", "62290");
        json2.put("signal_strength", -101);
        array.put(json2);
        
        json.put("cell_towers", array);

           2、組拼好的JSON字串:

 {
    "mobile_network_code":"0",
    "location_area_code":"6338",
    "host":"maps.google.com",
    "cell_towers":
        [
          {
             "signal_strength":-95,
             "cell_id":"62291"
          },
          {
              "signal_strength":-101,
              "cell_id":"62290"
           }
         ],
         "age":0,
         "mobile_country_code":"460",
         "version":"1.1.0"
 }

       3、通過HTTP請求返回的JSON形式結果:

   {
    "location":
        {
            "latitude":"0.0",
            "longitude":"0.0",
            "address":
            {
                "city":"基站資訊不存在,請從手機上讀取正確的基站資訊。",
                "country":"",
                "country_code":""
                ,"county":"",
                "postal_code":"",
                "region":"",
                "street":"",
                "street_number":""
             }
         },
         "access_token":"dummytoken"
   }


二、可行的方式:

        1、通過單個附近的基站資訊定位:

        組裝引數:

 /**
     * 獲取JSON形式的基站資訊
     * @param mcc 移動國家程式碼(中國的為460)
     * @param mnc 行動網路號碼(中國移動為0,中國聯通為1,中國電信為2); 
     * @param lac 位置區域碼
     * @param cid 基站編號
     * @param bsss 基站訊號強度
     * @return json
     * @throws JSONException
     */
    private String getJsonCellPos(int mcc, int mnc, int lac, int cid, int bsss) throws JSONException {
        JSONObject jsonCellPos = new JSONObject();
        jsonCellPos.put("version", "1.1.0");
        jsonCellPos.put("host", "maps.google.com");

        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("location_area_code", "" + lac + "");
        json1.put("mobile_country_code", "" + mcc + "");
        json1.put("mobile_network_code", "" + mnc + "");
        json1.put("age", 0);
        json1.put("cell_id", "" + cid + "");
        json1.put("signal_strength", bsss);
        array.put(json1);

        jsonCellPos.put("cell_towers", array);
        return jsonCellPos.toString();
    }

       請求的JSON字串:

          {
            "cell_towers":
                [
                 {
                     "mobile_network_code":"0",
                     "location_area_code":"6338",
                     "cell_id":"62291",
                     "signal_strength":-95,
                     "age":0,
                     "mobile_country_code":"460"
                  }
                 ],
             "host":"maps.google.com",
             "version":"1.1.0"
          }

          返回JSON形式結果:

        {
            "location":
            {
                "latitude":"31.214667405",
                "longitude":"121.59903152499999",
                "address":
                {
                    "city":"上海市浦東新區郭守敬路;上海奧威科技開發公司、科威國際技術轉移中心公司、張江高科技園區熱力中心[附近]",
                    "country":"",
                    "country_code":"",
                    "county":"",
                    "postal_code":"",
                    "region":"",
                    "street":"",
                    "street_number":""
                }
           },
           "access_token":"dummytoken"
        }

        2、通過多個附近的基站資訊定位:

              又一次猜想:

              請求JSON:

         {
            "cell_towers":
                [
                 {
                     "mobile_network_code":"0",
                     "location_area_code":"6338",
                     "cell_id":"62291",
                     "signal_strength":-95,
                     "age":0,
                     "mobile_country_code":"460"
                 },
                 {
                     "mobile_network_code":"0",
                     "location_area_code":"6338",
                     "cell_id":"62290",
                     "signal_strength":-101,
                     "age":1,
                     "mobile_country_code":"460"
                  }
                 ],
                 "host":"maps.google.com",
                 "version":"1.1.0"
         }

              返回JSON:

        {
            "location":
            {
                "latitude":"31.21485922285714",
                "longitude":"121.59990774285711",
                "address":
                {
                    "city":"上海市浦東新區郭守敬路276號;上海奧威科技開發公司、科威國際技術轉移中心公司、張江高科技園區熱力中心[附近]",
                    "country":"",
                    "country_code":"",
                    "county":"",
                    "postal_code":"",
                    "region":"",
                    "street":"",
                    "street_number":""
                 }
            },
            "access_token":"dummytoken"
        }

        結論:第三方伺服器端可能有限制,每次只接受JSONArray中的第一個元素。如果要想一次性查詢附近多個基站的經緯度及所在的地址,就只能編寫for迴圈,一次一次的訪問第三方的伺服器。

        3、個人觀點:所謂的三點定位或多點定位,只是讓想知道使用者當前位置的人多了一個選擇而已。三點定位得到的是三個點,並非是一個比較準確的點。