利用Google接口实现基站定位

[java]  view plain copy
  1. package lab.sodino.location;  
  2. import java.io.BufferedReader;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.InputStreamReader;  
  6. import java.io.UnsupportedEncodingException;  
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.client.ClientProtocolException;  
  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.JSONException;  
  15. import org.json.JSONObject;  
  16. import android.app.Activity;  
  17. import android.content.Context;  
  18. import android.os.Bundle;  
  19. import android.telephony.TelephonyManager;  
  20. import android.telephony.gsm.GsmCellLocation;  
  21. import android.view.View;  
  22. import android.widget.Button;  
  23. import android.widget.TextView;  
  24. /** 
  25.  * Google定位的实现.<br/> 
  26.  * Geolocation的详细信息请参见:<br/> 
  27.  * <a 
  28.  * href="http://code.google.com/apis/gears/geolocation_network_protocol.html"> 
  29.  * http://code.google.com/apis/gears/geolocation_network_protocol.html</a> 
  30.  */  
  31. public class LocationAct extends Activity {  
  32.     private TextView txtInfo;  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         Button btn = (Button) findViewById(R.id.btnStart);  
  37.         txtInfo = (TextView) findViewById(R.id.txtInfo);  
  38.         btn.setOnClickListener(new Button.OnClickListener() {  
  39.             public void onClick(View view) {  
  40.                 getLocation();  
  41.             }  
  42.         });  
  43.     }  
  44.     private void getLocation() {  
  45.         TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  46.         GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();  
  47.         int cid = gsmCell.getCid();  
  48.         int lac = gsmCell.getLac();  
  49.         String netOperator = tm.getNetworkOperator();  
  50.         int mcc = Integer.valueOf(netOperator.substring(03));  
  51.         int mnc = Integer.valueOf(netOperator.substring(35));  
  52.         JSONObject holder = new JSONObject();  
  53.         JSONArray array = new JSONArray();  
  54.         JSONObject data = new JSONObject();  
  55.         try {  
  56.             holder.put("version""1.1.0");  
  57.             holder.put("host""maps.google.com");  
  58.             holder.put("address_language""zh_CN");  
  59.             holder.put("request_address"true);  
  60.             holder.put("radio_type""gsm");  
  61.             holder.put("carrier""HTC");  
  62.             data.put("cell_id", cid);  
  63.             data.put("location_area_code", lac);  
  64.             data.put("mobile_countyr_code", mcc);  
  65.             data.put("mobile_network_code", mnc);  
  66.             array.put(data);  
  67.             holder.put("cell_towers", array);  
  68.         } catch (JSONException e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.         DefaultHttpClient client = new DefaultHttpClient();  
  72.         HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");  
  73.         StringEntity stringEntity = null;  
  74.         try {  
  75.             stringEntity = new StringEntity(holder.toString());  
  76.         } catch (UnsupportedEncodingException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.         httpPost.setEntity(stringEntity);  
  80.         HttpResponse httpResponse = null;  
  81.         try {  
  82.             httpResponse = client.execute(httpPost);  
  83.         } catch (ClientProtocolException e) {  
  84.             e.printStackTrace();  
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         HttpEntity httpEntity = httpResponse.getEntity();  
  89.         InputStream is = null;  
  90.         try {  
  91.             is = httpEntity.getContent();  
  92.         } catch (IllegalStateException e) {  
  93.             e.printStackTrace();  
  94.         } catch (IOException e) {  
  95.             // TODO Auto-generated catch block  
  96.             e.printStackTrace();  
  97.         }  
  98.         InputStreamReader isr = new InputStreamReader(is);  
  99.         BufferedReader reader = new BufferedReader(isr);  
  100.         StringBuffer stringBuffer = new StringBuffer();  
  101.         try {  
  102.             String result = "";  
  103.             while ((result = reader.readLine()) != null) {  
  104.                 stringBuffer.append(result);  
  105.             }  
  106.         } catch (IOException e) {  
  107.             e.printStackTrace();  
  108.         }  
  109.         System.out.println("[sodino]" + stringBuffer.toString());  
  110.         txtInfo.setText(stringBuffer.toString());  
  111.     }  
  112. }  

[xhtml]  view plain copy
  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>  
  2. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  

 

最后效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值