`
20386053
  • 浏览: 433199 次
文章分类
社区版块
存档分类
最新评论

通过SIM卡获取GPS,android基站定位原理

 
阅读更多
TelephonyManager telManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		GsmCellLocation glc=(GsmCellLocation) telManager.getCellLocation();
		int cid=glc.getCid();///gsm cell id, -1 if unknown, 0xffff max legal value 基站ID号
		int lac=glc.getLac();//写入区域代码
		String strOperator=telManager.getNetworkOperator();
		
		int mcc=Integer.valueOf(strOperator.substring(0, 3));//写入当前城市代码
		int mnc=Integer.valueOf(strOperator.substring(3, 5));//写入网络代码
		String getNumber="";
		getNumber+=("cid:"+cid+"\n");
		getNumber+=("cid:"+lac+"\n");
		getNumber+=("cid:"+mcc+"\n");
		getNumber+=("cid:"+mnc+"\n");
		
		try {
			JSONObject jObject=new JSONObject();
			jObject.put("version", "1.1.0");
			jObject.put("host", "maps.google.com");
			jObject.put("request_address", true);
			if(mcc==460)
				jObject.put("address_language","zh_CN");
			else
				jObject.put("address_language", "en_US");
			
			JSONArray jArray=new JSONArray();
			JSONObject jData=new JSONObject();
			jData.put("cell_id", cid);
			jData.put("location_area_code", lac);
			jData.put("mobile_country_code", mcc);
			jData.put("mobile_network_code", mnc);//
			jArray.put(jData);
			jObject.put("cell_towers",jArray);
			DefaultHttpClient client=new DefaultHttpClient();
			HttpPost post=new HttpPost("http://www.google.com/loc/json");
			StringEntity se=new StringEntity(jObject.toString());
			post.setEntity(se);
			
			HttpResponse resp=client.execute(post);
			BufferedReader br=null;
			if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
				StringBuffer sb=new StringBuffer();
				
				String result=br.readLine();
				while(result!=null)
				{
					sb.append(getNumber);
					sb.append(result);
					result=br.readLine();
				}
				
				String s=sb.toString();
				s=s.substring(s.indexOf("{"));
				btn.setText(s);
				JSONObject jo=new JSONObject(s);
				
				String values="";
				JSONObject arr= jo.getJSONObject("location");
				String lat= arr.get("latitude").toString();
				String lon=arr.getString("longitude").toString();
				
				
				Toast.makeText(getApplicationContext(), "经度:"+lon+" 纬度:"+lat,1).show();
				
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
最后还要加入网络访问权限
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics