CDMA基站定位获取经纬度

这几天在做基站定位,发觉CDMA的基站定位在网上资料很少。经过漫长的摸索,其中的小小收获给大家分享一下!
这个是CDMA中国电信的基站定位获取经纬度的源码及测试截图!在模拟器中用似乎不行,模拟器的网络不是CDMA的。有知道的请指点指点!
  

测试程序源码:    netWorkLocationTest2.rar 

主要代码:
  1. public void onClick(View v) {
  2.                         // TODO Auto-generated method stub
  3.                         tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  4.                         //int type = tm.getNetworkType();
  5.                         //if (type ==TelephonyManager.NETWORK_TYPE_CDMA) {
  6.                                 location = (CdmaCellLocation) tm.getCellLocation();
  7.                                 if(location == null)
  8.                                         return;
  9.                                 int sid = location.getSystemId();//系统标识  mobileNetworkCode
  10.                                 int bid = location.getBaseStationId();//基站小区号  cellId
  11.                                 int nid = location.getNetworkId();//网络标识  locationAreaCode
  12.                                 
  13.                                 Log.i(\"sid:\", \"\" + sid);
  14.                                 Log.i(\"bid:\", \"\" + bid);
  15.                                 Log.i(\"nid:\", \"\" + nid);
  16.                                 ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
  17.                                 CellIDInfo info = new CellIDInfo();
  18.                                 info.cellId = bid;
  19.                                 info.locationAreaCode = nid;
  20.                                 info.mobileNetworkCode = String.valueOf(sid);
  21.                                 info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
  22.                                 info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
  23.                                 info.radioType = \"cdma\";
  24.                                 CellID.add(info);
  25.                                 Log.d(\"cellId:\", \"\" + info.cellId);
  26.                                 Log.d(\"locationAreaCode:\", \"\" + info.locationAreaCode);
  27.                                 Log.d(\"mobileNetworkCode:\", info.mobileNetworkCode);
  28.                                 Log.d(\"mobileCountryCode:\", info.mobileCountryCode);
  29.                                 Location loc = callGear(CellID);
  30.                                 
  31.                                 if (loc !=null) 
  32.                                         mTextView.setText(\"纬度:\" + loc.getLatitude() + \"\\n经度:\" + loc.getLongitude());
  33.                         //}// end if
  34.                 }// end onclick
复制代码

调用google gears的方法,该方法调用gears来获取经纬度 代码:
  1. //调用google gears的方法,该方法调用gears来获取经纬度 
  2.         private Location callGear(ArrayList<CellIDInfo> cellID) {
  3.             if (cellID == null) 
  4.                     return null;
  5.             
  6.             DefaultHttpClient client = new DefaultHttpClient();
  7.                 HttpPost post = new HttpPost(
  8.                                 \"http://www.google.com/loc/json\");
  9.                 JSONObject holder = new JSONObject();

  10.                 try {
  11.                         holder.put(\"version\", \"1.1.0\");
  12.                         holder.put(\"host\", \"maps.google.com\");
  13.                         holder.put(\"home_mobile_country_code\", cellID.get(0).mobileCountryCode);
  14.                         holder.put(\"home_mobile_network_code\", cellID.get(0).mobileNetworkCode);
  15.                         holder.put(\"radio_type\", cellID.get(0).radioType);
  16.                         holder.put(\"request_address\", true);
  17.                         if (\"460\".equals(cellID.get(0).mobileCountryCode)) 
  18.                                 holder.put(\"address_language\", \"zh_CN\");
  19.                         else
  20.                                 holder.put(\"address_language\", \"en_US\");
  21.                         
  22.                         JSONObject data,current_data;

  23.                         JSONArray array = new JSONArray();
  24.                         
  25.                         current_data = new JSONObject();
  26.                         current_data.put(\"cell_id\", cellID.get(0).cellId);
  27.                         current_data.put(\"location_area_code\", cellID.get(0).locationAreaCode);
  28.                         current_data.put(\"mobile_country_code\", cellID.get(0).mobileCountryCode);
  29.                         current_data.put(\"mobile_network_code\", cellID.get(0).mobileNetworkCode);
  30.                         current_data.put(\"age\", 0);
  31.                         current_data.put(\"signal_strength\", -60);
  32.                         current_data.put(\"timing_advance\", 5555);
  33.                         array.put(current_data);
  34.                         
  35.                         holder.put(\"cell_towers\", array);
  36.                                                 
  37.                         StringEntity se = new StringEntity(holder.toString());
  38.                         Log.e(\"Location send\", holder.toString());
  39.                         post.setEntity(se);
  40.                         HttpResponse resp = client.execute(post);

  41.                         HttpEntity entity = resp.getEntity();

  42.                         BufferedReader br = new BufferedReader(
  43.                                         new InputStreamReader(entity.getContent()));
  44.                         StringBuffer sb = new StringBuffer();
  45.                         String result = br.readLine();
  46.                         while (result != null) {
  47.                                 Log.e(\"Locaiton reseive\", result);
  48.                                 sb.append(result);
  49.                                 result = br.readLine();
  50.                         }

  51.                         data = new JSONObject(sb.toString());
  52.                         Log.d(\"-\", sb.toString());
  53.                         data = (JSONObject) data.get(\"location\");

  54.                         Location loc = new Location(LocationManager.NETWORK_PROVIDER);
  55.                         loc.setLatitude((Double) data.get(\"latitude\"));
  56.                         loc.setLongitude((Double) data.get(\"longitude\"));
  57.                         loc.setAccuracy(Float.parseFloat(data.get(\"accuracy\").toString()));
  58.                         loc.setTime( System.currentTimeMillis());//AppUtil.getUTCTime());
  59.                         return loc;
  60.                 } catch (JSONException e) {
  61.                         e.printStackTrace();
  62.                         return null;
  63.                 } catch (UnsupportedEncodingException e) {
  64.                         e.printStackTrace();
  65.                 } catch (ClientProtocolException e) {
  66.                         e.printStackTrace();
  67.                 } catch (IOException e) {
  68.                         e.printStackTrace();
  69.                 }
  70.                 Log.d(\"-\", \"null 1\");
  71.                 return null;
  72.         }
复制代码

在弄这个的时候不要忘了加权限噢!
  1. <uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"></uses-permission>
  2. <uses-permission android:name=\"android.permission.READ_PHONE_STATE\"></uses-permission>
  3. <uses-permission android:name=\"android.permission.INTERNET\"></uses-permission>
复制代码

程序只是测试用,尚不完善!希望这个东东对需要的朋友有用!有什么不对的地方请大家指点指点。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值