Android 基于GeolocationAPI的基站定位


api 地址为 http://code.google.com/p/gears/wiki/GeolocationAPI

发送的格式:

                              {
 "location": {
   "latitude": 51.0,
   "longitude": -0.1,
   "altitude": 30.1,
   "accuracy": 1200.1,
   "altitude_accuracy": 10.1,
   "address": {
     "street_number": "100",
     "street": "Amphibian Walkway",
     "postal_code": "94043",
     "city": "Mountain View",
     "county": "Mountain View County",
     "region": "California",
     "country": "United States of America",
     "country_code": "US"
   }
 }
}

返回的格式:              {
 "location": {
   "latitude": 51.0,
   "longitude": -0.1,
   "altitude": 30.1,
   "accuracy": 1200.1,
   "altitude_accuracy": 10.1,
   "address": {
     "street_number": "100",
     "street": "Amphibian Walkway",
     "postal_code": "94043",
     "city": "Mountain View",
     "county": "Mountain View County",
     "region": "California",
     "country": "United States of America",
     "country_code": "US"
   }
 }
}

  得到LAC 和CellId后,其它的就是json解析了;

  1.   
  2. import java.io.BufferedReader;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.net.URL;  
  6. import java.util.Date;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.client.methods.HttpPost;  
  11. import org.apache.http.entity.StringEntity;  
  12. import org.apache.http.impl.client.DefaultHttpClient;  
  13. import org.json.JSONArray;  
  14. import org.json.JSONObject;  
  15.   
  16.   
  17. import Android.app.Activity;  
  18. import android.content.Context;  
  19. import android.os.Bundle;  
  20. import android.telephony.TelephonyManager;  
  21. import android.telephony.gsm.GsmCellLocation;  
  22. import android.view.View;  
  23. import android.widget.Button;  
  24. import android.widget.TextView;  
  25.   
  26. public class LocationStation extends Activity {  
  27.     TextView mTextView;  
  28.     Button mButton;  
  29.     TelephonyManager tm;  
  30.   
  31.     /** Called when the activity is first created. */  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.   
  37.         mTextView = (TextView) findViewById(R.id.textView);  
  38.         mButton = (Button) findViewById(R.id.Button);  
  39.         tm = (TelephonyManager) this  
  40.                 .getSystemService(Context.TELEPHONY_SERVICE);  
  41.   
  42.         mButton.setOnClickListener(new Button.OnClickListener() {  
  43.   
  44.             @Override  
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub   
  47.                 GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();  
  48.                 int cid = gcl.getCid();   
  49.                 int lac = gcl.getLac();  
  50.                 System.out.println("operator"+tm.getNetworkOperator()); //中国移动43600   
  51.                 int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,  
  52.                         3));  
  53.                 int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,  
  54.                         5));  
  55.                 /* 
  56.                 * 发送的格式:{ 
  57.                             "version": "1.1.0" , 
  58.                             "host": "maps.google.com", 
  59.                             "access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe", 
  60.                             "home_mobile_country_code": 460, 
  61.                             "home_mobile_network_code":0, 
  62.                             "address_language": "zh_CN", 
  63.                             "radio_type": "gsm", 
  64.                             "request_address": true , 
  65.                             "cell_towers":[ 
  66.                             { 
  67.                             "cell_id":36526, 
  68.                             "location_area_code":14556, 
  69.                             "mobile_country_code":460, 
  70.                             "mobile_network_code":0, 
  71.                             "timing_advance":5555 
  72.                             } 
  73.                             ] 
  74.                             } 
  75.                  */  
  76.                 try {  
  77.                     // 组装JSON查询字符串   
  78.                     JSONObject holder = new JSONObject();  
  79.                     holder.put("version""1.1.0");  
  80.                     holder.put("host""maps.google.com");  
  81.                     holder.put("address_language""zh_CN");  
  82.                     holder.put("request_address"true);  
  83.   
  84.                     JSONArray array = new JSONArray();  
  85.                     JSONObject data = new JSONObject();  
  86.                     data.put("cell_id", cid); // 25070   
  87.                     data.put("location_area_code", lac);// 4474   
  88.                     data.put("mobile_country_code", mcc);// 460   
  89.                     data.put("mobile_network_code", mnc);// 0   
  90.                     array.put(data);  
  91.                     holder.put("cell_towers", array);  
  92.   
  93.                     // 创建连接,发送请求并接受回应   
  94.                     DefaultHttpClient client = new DefaultHttpClient();  
  95.   
  96.                     HttpPost post = new HttpPost(  
  97.                             "http://www.google.com/loc/json");  
  98.   
  99.                     StringEntity se = new StringEntity(holder.toString());  
  100.   
  101.                     post.setEntity(se);  
  102.                     HttpResponse resp = client.execute(post);  
  103.   
  104.                     HttpEntity entity = resp.getEntity();  
  105.   
  106.                     BufferedReader br = new BufferedReader(  
  107.                             new InputStreamReader(entity.getContent()));  
  108.                     StringBuffer sb = new StringBuffer();  
  109.                     String result = br.readLine();  
  110.   
  111.                     while (result != null) {  
  112.   
  113.                         sb.append(result);  
  114.                         result = br.readLine();  
  115.                     }  
  116.                     /* 
  117.                      * 返回格式:   { 
  118.                           "location": { 
  119.                             "latitude": 51.0, 
  120.                             "longitude": -0.1, 
  121.                             "altitude": 30.1, 
  122.                             "accuracy": 1200.1, 
  123.                             "altitude_accuracy": 10.1, 
  124.                             "address": { 
  125.                               "street_number": "100", 
  126.                               "street": "Amphibian Walkway", 
  127.                               "postal_code": "94043", 
  128.                               "city": "Mountain View", 
  129.                               "county": "Mountain View County", 
  130.                               "region": "California", 
  131.                               "country": "United States of America", 
  132.                               "country_code": "US" 
  133.                             } 
  134.                           } 
  135.                         } 
  136.                      */  
  137.                     JSONObject jsonObject = new JSONObject(sb.toString());  
  138.   
  139.                     JSONObject jsonObject1 = new JSONObject(jsonObject  
  140.                             .getString("location"));  
  141.   
  142.                     getAddress(jsonObject1.getString("latitude"), jsonObject1  
  143.                             .getString("longitude"));  
  144.   
  145.                     //mTextView.setText(sb.toString());   
  146.                 } catch (Exception e) {  
  147.                     // TODO: handle exception   
  148.                 }  
  149.   
  150.             }  
  151.   
  152.         });  
  153.     }  
  154.   
  155.     void getAddress(String lat, String lag) {  
  156.         try {  
  157.   
  158.             URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="  
  159.                     + lat + "," + lag);  
  160.             InputStream inputStream = url.openConnection().getInputStream();  
  161.             InputStreamReader inputReader = new InputStreamReader(inputStream,  
  162.                     "utf-8");  
  163.             BufferedReader bufReader = new BufferedReader(inputReader);  
  164.   
  165.             String line = "", lines = "";  
  166.   
  167.             while ((line = bufReader.readLine()) != null) {  
  168.                 lines += line;  
  169.             }  
  170.             if (!lines.equals("")) {  
  171.   
  172.                 JSONObject jsonobject = new JSONObject(lines);  
  173.                 JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")  
  174.                         .toString());  
  175.                 for (int i = 0; i < jsonArray.length(); i++) {  
  176.   
  177.                     mTextView.setText( jsonArray.getJSONObject(i).getString("address"));  
  178.                               
  179.                 }  
  180.   
  181.             }  
  182.   
  183.         } catch (Exception e) {  
  184.             ;  
  185.         }  
  186.   
  187.     }  
  188. }  

相关内容