Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 基站定位實戰:從單基站走向三基站(多基站)定位

編輯:Android 開發實例

這一篇是上一篇《Android 基站定位之實現單基站定位》的延伸。單基站只能告訴你「大概在哪片覆蓋區」,而想讓結果更靠近真實位置,自然會想到把周邊好幾座基站的資訊一起丟給定位服務去推算。本文就把這條路走一遍,包括踩到的坑。

先把名詞說清楚

基站:可以把它想成一個巨大的無線網路接入點。官方說法是行動通訊系統中連接固定網路與無線網路、透過空中無線電波與行動台溝通的設備;也就是在某一無線電覆蓋範圍內,經由行動通訊交換中心與手機終端之間傳遞訊息的無線電收發信電台。

單基站定位:只拿手機「目前實際連上」的那一座基站資訊來推位置,結論是使用者落在該基站無線電覆蓋區中的某處。

三基站/多基站定位:拿的不是目前連接的那一座,而是周邊一整組基站資訊,讓服務端用多組訊號一起運算,理論上推估結果會比單基站更精準。

一、取得鄰區基站資訊

// 取得鄰區基站資訊
List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();

StringBuffer sb = new StringBuffer("總數: " + infos.size() + "\n");
for (NeighboringCellInfo info : infos) {   // 依鄰區數量循環
    sb.append(" LAC: " + info.getLac());   // 當前鄰區的位置區域碼
    sb.append(" CID: " + info.getCid());   // 當前鄰區的基站編號
    sb.append(" BSSS: " + (-113 + 2 * info.getRssi()) + "\n"); // 換算後的訊號強度
}

實測提醒:中國聯通的用戶幾乎拿不到鄰區基站資料,我測試時用的是中國移動的門號,才能穩定取到多筆鄰區。

二、摸索請求參數的組成

既然單基站那套 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);

組出的字串長這樣:

{
  "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"
}

但 HTTP 打回去之後,換回來的是這麼一份回應:

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

很明顯,這個組法服務端並不買單。

三、真正可行的做法

1. 只送一座鄰區基站

把 MCC、MNC、LAC、CID、訊號強度包成單筆的 cell_towers 陣列,就能正常換回座標。

/**
 * 組出 JSON 形式的基站資訊
 *
 * @param mcc 行動國家代碼(台灣/中國都是 460 起頭的段位,中國為 460)
 * @param mnc 行動網路碼(中國移動 0、中國聯通 1、中國電信 2)
 * @param lac 位置區域碼
 * @param cid 基站編號
 * @param bsss 基站訊號強度
 * @return JSON 字串
 */
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();
}

送出的請求本文:

{
  "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"
}

回應就有料了:

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

2. 一次送多座鄰區基站

再猜一次:把同一組欄位複製成多筆塞進 cell_towers。

{
  "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"
}

結果回來的是:

{
  "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 迴圈,一座一座地拜訪服務端,再把結果收集起來。

四、我的看法

所謂三點定位、多點定位,真正的意義是「多給想知道你位置的人一個選項」,而不是把誤差縮小到一個點。三點定位得到的是三個點、三組座標,它本質上仍是一組各自獨立的推估值,需要你自己決定怎麼加權、怎麼取平均,服務端不會替你變出一個更準的單一答案。

測試網址:https://www.minigps.net/map3.html

原文轉自:https://blog.csdn.net/android_ls/article/details/8673532

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved