网络上多数教程是讲GSM网获取基站的,而忽略了C网的基站
这里我们可以比较一下GSM 和 CDMA 在获取基站信息时的不同之处
GSM:
int lac = gsm.getLac();
String mcc = manager.getNetworkOperator().substring(0, 3);
String mnc = manager.getNetworkOperator().substring(3, 5);
int cid = gsm.getCid();
CDMA:
int lac = cdma.getNetworkId();
String mcc = manager.getNetworkOperator().substring(0, 3);
String mnc = String.valueOf(cdma.getSystemId());
int cid = cdma.getBaseStationId();
在获取区域码LAC时GSM使用的是GsmCellLocation.getLac(),CDMA则用CdmaCellLocation.getNetworkId()来代替
在获取基站ID时GSM使用的是GsmCellLocation.getCid(),CDMA则用CdmaCellLocation.getBaseStationId()来代替
前面获取到的都是单个基站的信息,后面再获取周围邻近基站信息以辅助通过基站定位的精准性
TelephonyManager.getNeighboringCellInfo(),将其也放入基站信息LIST表中
最后通过google提供的gear接口获取经纬度,代码如下:
- public static Location callGear(List<CellIDInfo> cellID) {
- if (cellID == null || cellID.size() == 0)
- return null;
- DefaultHttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost("http://www.google.com/loc/json");
- JSONObject holder = new JSONObject();
- try {
- holder.put("version", "1.1.0");
- holder.put("host", "maps.google.com");
- holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
- holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
- holder.put("radio_type", cellID.get(0).radioType);
- holder.put("request_address", true);
- if ("460".equals(cellID.get(0).mobileCountryCode))
- holder.put("address_language", "zh_CN");
- else
- holder.put("address_language", "en_US");
- JSONObject data,current_data;
- JSONArray array = new JSONArray();
- current_data = new JSONObject();
- current_data.put("cell_id", cellID.get(0).cellId);
- current_data.put("location_area_code", cellID.get(0).locationAreaCode);
- current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
- current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
- current_data.put("age", 0);
- current_data.put("signal_strength", -60);
- current_data.put("timing_advance", 5555);
- array.put(current_data);
- if (cellID.size() > 2) {
- for (int i = 1; i < cellID.size(); i++) {
- data = new JSONObject();
- data.put("cell_id", cellID.get(i).cellId);
- data.put("location_area_code", cellID.get(i).locationAreaCode);
- data.put("mobile_country_code", cellID.get(i).mobileCountryCode);
- data.put("mobile_network_code", cellID.get(i).mobileNetworkCode);
- data.put("age", 0);
- array.put(data);
- }
- }
- holder.put("cell_towers", array);
- StringEntity se = new StringEntity(holder.toString());
- Log.e("Location send", holder.toString());
- post.setEntity(se);
- HttpResponse resp = client.execute(post);
- HttpEntity entity = resp.getEntity();
- BufferedReader br = new BufferedReader(
- new InputStreamReader(entity.getContent()));
- StringBuffer sb = new StringBuffer();
- String result = br.readLine();
- while (result != null) {
- Log.e("Locaiton reseive-->", result);
- sb.append(result);
- result = br.readLine();
- }
- data = new JSONObject(sb.toString());
- data = (JSONObject) data.get("location");
- Location loc = new Location(LocationManager.NETWORK_PROVIDER);
- loc.setLatitude((Double) data.get("latitude"));
- loc.setLongitude((Double) data.get("longitude"));
- loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
- loc.setTime( System.currentTimeMillis());//AppUtil.getUTCTime());
- return loc;
- } catch (JSONException e) {
- e.printStackTrace();
- return null;
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
大家注意看这行holder.put("radio_type", cellID.get(0).radioType);
GSM就用"gsm",CDMA就用"cdma"
这个千万别搞混了,不然就获取不到信息了
