当前位置:操作系统 > 安卓/Android >>

Android基站定位——单基站定位 (二)

基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值及地址信息(大概位置)。
 一、通过手机信号获取基站信息 
[java] www.zzzyk.com
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  
               // 返回值MCC + MNC  
               String operator = mTelephonyManager.getNetworkOperator();  
               mcc = Integer.parseInt(operator.substring(0, 3));  
               mnc = Integer.parseInt(operator.substring(3));  
  
               // 中国移动和中国联通获取LAC、CID的方式  
               GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  
               lac = location.getLac();  
               cid = location.getCid();  
  
               Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);  
二、调用第三方公开的API(根据基站信息查找基站的经纬度值及地址信息)
      1、组拼JSON形式的请求参数
[java]  
/** 
    * 获取JSON形式的基站信息 
    * @param mcc 移动国家代码(中国的为460) 
    * @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2);  
    * @param lac 位置区域码 
    * @param cid 基站编号 
    * @return json 
    * @throws JSONException 
    */  
   private String getJsonCellPos(int mcc, int mnc, int lac, int cid) 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 + "");  
       array.put(json1);  
  
       jsonCellPos.put("cell_towers", array);  
       return jsonCellPos.toString();  
   }  
      2、通过HTTP协议网络请求源码: 
[plain]  
request URL:http://www.minigps.net/minigps/map/google/location  
Request Method:POST  
Status Code:200 OK  
Request Headersview source  
Accept:application/json, text/javascript, */*; q=0.01  
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3  
Accept-Encoding:gzip,deflate,sdch  
Accept-Language:zh-CN,zh;q=0.8  
Connection:keep-alive  
Content-Length:191  
Content-Type:application/json; charset=UTF-8  
Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372  
Host:www.minigps.net  
Origin:http://www.minigps.net  
Referer:http://www.minigps.net/map.html  
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4  
X-Requested-With:XMLHttpRequest  
Request Payload  
{"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}  
  
Response Headersview source  
Content-Type:application/json  
Date:Sat, 03 Jan 2013 14:03:15 GMT  
Server:Apache-Coyote/1.1  
Transfer-Encoding:chunked  
      
        3、用JAVA代码具体实现:
[java]  
/** 
    * 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息 
    */  
   public String httpPost(String url, String jsonCellPos) throws IOException{  
       byte[] data = jsonCellPos.toString().getBytes();  
       URL realUrl = new URL(url);  
       HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();  
       httpURLConnection.setConnectTimeout(6 * 1000);  
       httpURLConnection.setDoOutput(true);  
       httpURLConnection.setDoInput(true);  
       httpURLConnection.setUseCaches(false);  
       httpURLConnection.setRequestMethod("POST");  
       httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");  
       httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");  
       httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");  
       httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");  
       httpURLConnection.setRequestProperty("Connection", "Keep-Alive");  
       httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));  
       httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");  
  
       httpURLConnection.setRequestProperty("Host", "www.minigps.net");  
       httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");  
       httpURLConnection.setRequestProperty("User-Agent",  
               "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/5
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,