基站定位

最近在做基站定位,移动电信的定位测试都可以。就是联通的定位不了。在这里把源码上传,希望各位帮忙找出原因。
标签: <无>

代码片段(4)

[图片] android 基站定位

[文件] GetBaseStationInfo.zip ~ 62KB    下载(276)

[代码] android 基站定位

001 public class Get {
002  
003     public static class CellIDInfo {
004          
005         public int cellId;
006         public String mobileCountryCode;
007         public String mobileNetworkCode;
008         public int locationAreaCode;
009         public String radioType;
010         public CellIDInfo(){};
011     }
012      
013      
014      
015      public Location callGear(ArrayList<CellIDInfo> cellID) {
016 //       System.out.println(cellID.get(0));
017             if (cellID.isEmpty()) {
018                 return null;
019             }
020             DefaultHttpClient client = new DefaultHttpClient();
021             HttpPost post = new HttpPost(
022                     "http://www.google.com/loc/json");
023             JSONObject holder = new JSONObject();
024             try {
025                 holder.put("version", "1.1.0");
026                 holder.put("host", "maps.google.com");
027                 holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
028                 holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
029                 holder.put("radio_type", cellID.get(0).radioType);
030                 holder.put("request_address", true);
031                 if ("460".equals(cellID.get(0).mobileCountryCode))
032                     holder.put("address_language", "zh_CN");
033                 else
034                     holder.put("address_language", "en_US");
035                 JSONObject data,current_data;
036                 JSONArray array = new JSONArray();
037                 current_data = new JSONObject();
038                 current_data.put("cell_id", cellID.get(0).cellId);
039                 current_data.put("location_area_code", cellID.get(0).locationAreaCode);
040                 current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
041                 current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
042                 current_data.put("age", 0);
043                 array.put(current_data);
044                 if (cellID.size() > 2) {
045                     for (int i = 1; i < cellID.size(); i++) {
046                         data = new JSONObject();
047                         data.put("cell_id", cellID.get(i).cellId);
048                         data.put("location_area_code", cellID.get(i).locationAreaCode);
049                         data.put("mobile_country_code", cellID.get(i).mobileCountryCode);
050                         data.put("mobile_network_code", cellID.get(i).mobileNetworkCode);
051                         data.put("age", 0);
052                         array.put(data);
053                     }
054                 }
055                 holder.put("cell_towers", array);
056                 StringEntity se = new StringEntity(holder.toString());
057                 Log.e("Location send", holder.toString());
058                 post.setEntity(se);
059                 HttpResponse resp = client.execute(post);
060                 HttpEntity entity = resp.getEntity();
061  
062                 BufferedReader br = new BufferedReader(
063                         new InputStreamReader(entity.getContent()));
064                 StringBuffer sb = new StringBuffer();
065                 String result = br.readLine();
066                 while (result != null) {
067                     Log.e("Locaiton receive", result);
068                     sb.append(result);
069                     result = br.readLine();
070                 }
071                 if(sb.length() <= 1)
072                     return null;
073                 data = new JSONObject(sb.toString());
074                 data = (JSONObject) data.get("location");
075  
076                 Location loc = new Location(LocationManager.NETWORK_PROVIDER);
077                 loc.setLatitude((Double) data.get("latitude"));
078                 loc.setLongitude((Double) data.get("longitude"));
079                 loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
080                 loc.setTime(GetUTCTime());
081                 return loc;
082             } catch (JSONException e) {
083                 return null;
084             } catch (UnsupportedEncodingException e) {
085                 e.printStackTrace();
086             } catch (ClientProtocolException e) {
087                 e.printStackTrace();
088             } catch (IOException e) {
089                 e.printStackTrace();
090             }
091             return null;
092         }
093          
094         /**
095          * 获取地理位置
096          *
097          * @throws Exception
098          */
099         public String getLocation(Location itude) throws Exception {
100             String resultString = "";
101  
102             /** 这里采用get方法,直接将参数加到URL上 */
103             String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.getLatitude(), itude.getLongitude());
104             Log.i("URL", urlString);
105  
106             /** 新建HttpClient */
107             HttpClient client = new DefaultHttpClient();
108             /** 采用GET方法 */
109             HttpGet get = new HttpGet(urlString);
110             try {
111                 /** 发起GET请求并获得返回数据 */
112                 HttpResponse response = client.execute(get);
113                 HttpEntity entity = response.getEntity();
114                 BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
115                 StringBuffer strBuff = new StringBuffer();
116                 String result = null;
117                 while ((result = buffReader.readLine()) != null) {
118                     strBuff.append(result);
119                 }
120                 resultString = strBuff.toString();
121  
122                 /** 解析JSON数据,获得物理地址 */
123                 if (resultString != null && resultString.length() > 0) {
124                     JSONObject jsonobject = new JSONObject(resultString);
125                     JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
126                     resultString = "";
127                     for (int i = 0; i < jsonArray.length(); i++) {
128                         resultString = jsonArray.getJSONObject(i).getString("address");
129                     }
130                 }
131             } catch (Exception e) {
132                 throw new Exception("获取物理位置出现错误:" + e.getMessage());
133             } finally {
134                 get.abort();
135                 client = null;
136             }
137  
138             return resultString;
139         }
140          
141         public long GetUTCTime() {
142             Calendar cal = Calendar.getInstance(Locale.CHINA);
143             int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
144             int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
145             cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
146             return cal.getTimeInMillis();
147         }
148      
149 }

[代码] [其他]代码

001 public class Test extends Activity {
002      
003  
004     private CdmaCellLocation location = null;
005     private Button btnGetInfo = null;
006      
007      
008     /** Called when the activity is first created. */
009     @Override
010     public void onCreate(Bundle savedInstanceState) {
011         super.onCreate(savedInstanceState);
012         setContentView(R.layout.main);
013          
014         final Get get = new Get();
015  
016         btnGetInfo = (Button)findViewById(R.id.btnGet);
017         btnGetInfo.setOnClickListener(new View.OnClickListener() {
018             @Override
019             public void onClick(View v) {
020                 EditText tv = (EditText)findViewById(R.id.editText1);
021                 // TODO Auto-generated method stub
022                 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
023                 int type = tm.getNetworkType();
024                 //在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA,电信的3G为EVDO
025                 //String OperatorName = tm.getNetworkOperatorName();
026                 Location loc = null;
027                 ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
028                 //中国电信为CTC
029                 //NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType
030                 //NETWORK_TYPE_CDMA电信2G是CDMA
031                 if (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT)
032                 {
033                     location = (CdmaCellLocation) tm.getCellLocation();
034                     int cellIDs = location.getBaseStationId();
035                     int networkID = location.getNetworkId();
036                     StringBuilder nsb = new StringBuilder();
037                     nsb.append(location.getSystemId());
038                     CellIDInfo info = new CellIDInfo();
039                     info.cellId = cellIDs;
040                     info.locationAreaCode = networkID; //ok
041                     info.mobileNetworkCode = nsb.toString();
042                     info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
043                     info.radioType = "cdma";
044                     CellID.add(info);
045                 }
046                 //移动2G卡 + CMCC + 2
047                 //type = NETWORK_TYPE_EDGE
048                 else if(type == TelephonyManager.NETWORK_TYPE_EDGE)
049                 {
050                     GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 
051                     int cellIDs = location.getCid(); 
052                     int lac = location.getLac();
053                     CellIDInfo info = new CellIDInfo();
054                     info.cellId = cellIDs;
055                     info.locationAreaCode = lac;
056                     info.mobileNetworkCode = tm.getNetworkOperator().substring(3, 5);  
057                     info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
058                     info.radioType = "gsm";
059                     CellID.add(info);
060                 }
061                 //联通的2G经过测试 China Unicom   1 NETWORK_TYPE_GPRS
062                 else if(type == TelephonyManager.NETWORK_TYPE_GPRS)
063                 {
064                     GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 
065                     int cellIDs = location.getCid(); 
066                     int lac = location.getLac();
067                     CellIDInfo info = new CellIDInfo();
068                     info.cellId = cellIDs;
069                     info.locationAreaCode = lac;
070                     //经过测试,获取联通数据以下两行必须去掉,否则会出现错误,错误类型为JSON Parsing Error
071                     //info.mobileNetworkCode = tm.getNetworkOperator().substring(0, 3);  
072                     //info.mobileCountryCode = tm.getNetworkOperator().substring(3);
073                     info.radioType = "gsm";
074                     CellID.add(info);
075                 }
076                 else
077                 {
078                     tv.setText("Current Not Support This Type.");
079                 }
080                  
081                 loc = get.callGear(CellID);
082                 //callGear(CellID);
083                  
084                 if(loc != null)
085                 {
086                     try {
087                          
088                         StringBuilder sb = new StringBuilder();
089                         String pos =  get.getLocation(loc);
090                         sb.append("CellID:");
091                         sb.append(CellID.get(0).cellId);
092                         sb.append("+\n");
093                          
094                         sb.append("home_mobile_country_code:");
095                         sb.append(CellID.get(0).mobileCountryCode);
096                         sb.append("++\n");
097                          
098                         sb.append("mobileNetworkCode:");
099                         sb.append(CellID.get(0).mobileNetworkCode);
100                         sb.append("++\n");
101                          
102                         sb.append("locationAreaCode:");
103                         sb.append(CellID.get(0).locationAreaCode);
104                         sb.append("++\n");
105                         sb.append(pos);
106                          
107                         tv.setText(sb.toString());
108                          
109                          
110                     } catch (Exception e) {
111                         // TODO Auto-generated catch block
112                         e.printStackTrace();
113                     }
114                 }
115             }
116         });
117          
118     }
119 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值