Android获取基站坐标代码


Android获取基站坐标代码:

  1. package com.su.station;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.client.methods.HttpPost;  
  10. import org.apache.http.entity.StringEntity;  
  11. import org.apache.http.impl.client.DefaultHttpClient;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14.   
  15. import android.app.Activity;  
  16. import android.content.Context;  
  17. import android.location.Location;  
  18. import android.location.LocationManager;  
  19. import android.os.Bundle;  
  20. import android.telephony.TelephonyManager;  
  21. import android.telephony.gsm.GsmCellLocation;  
  22. import android.util.Log;  
  23. import android.widget.Toast;  
  24.   
  25. public class TestStationLocationActivity extends Activity {  
  26.     private static final String TAG = "TestStationLocationActivity";  
  27.   
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         Location location = getportLocation();  
  34.         location.getLongitude();  
  35.         Toast.makeText(this, location.getLatitude()+location.getLatitude()+""100).show();  
  36.           
  37.     }  
  38.       
  39.     private Location getportLocation() {  
  40.         Location loc = null ;  
  41.         TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  42.         BufferedReader br = null;  
  43.         try   
  44.         {     
  45.             GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();  
  46.                 if (null == gcl)  
  47.                 {  
  48.                         return null;  
  49.                 }  
  50.                     int cid = gcl.getCid();  
  51.                     int lac = gcl.getLac();  
  52.                     int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));  
  53.                     int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));  
  54.                     JSONObject holder = new JSONObject();  
  55.                     holder.put("version""1.1.0");  
  56.                     holder.put("host""maps.google.com");  
  57.                     holder.put("request_address"true);  
  58.                       
  59.                     JSONArray array = new JSONArray();  
  60.                     JSONObject data = new JSONObject();  
  61.                       
  62.                     data.put("cell_id", cid);  
  63.                     data.put("location_area_code", lac);  
  64.                     data.put("mobile_country_code", mcc);  
  65.                     data.put("mobile_network_code", mnc);  
  66.                     array.put(data);  
  67.                     holder.put("cell_towers", array);  
  68.                     DefaultHttpClient client = new DefaultHttpClient();  
  69.                     HttpPost post = new HttpPost("http://www.google.com/loc/json");  
  70.                     StringEntity se = new StringEntity(holder.toString());  
  71.                     post.setEntity(se);  
  72.                     HttpResponse resp = client.execute(post);  
  73.                     if (resp.getStatusLine().getStatusCode() == 200)   
  74.                 {  
  75.                         HttpEntity entity = resp.getEntity();  
  76.                             br = new BufferedReader(new InputStreamReader(entity.getContent()));  
  77.                             StringBuffer sb = new StringBuffer();  
  78.                             String result = br.readLine();  
  79.                             while (result != null)   
  80.                             {  
  81.                                     sb.append(result);  
  82.                                     result = br.readLine();  
  83.                             }  
  84.                               
  85.                             JSONObject data_ = new JSONObject(sb.toString());  
  86.                             data_ = (JSONObject) data_.get("location");  
  87.                         loc = new Location(LocationManager.NETWORK_PROVIDER);  
  88.                         loc.setLatitude((Double) data_.get("latitude"));  
  89.                         loc.setLongitude((Double) data_.get("longitude"));  
  90.                         Log.i(TAG, "latitude : " + loc.getLatitude() + "  longitude : " + loc.getLongitude());  
  91.                         return loc;  
  92.                 }  
  93.                 return null;  
  94.   
  95.         }   
  96.           
  97.         catch (Exception e)   
  98.         {  
  99.                 android.util.Log.e(TAG, "network get the latitude and longitude ocurr Exception error", e);  
  100.         }  
  101.         finally  
  102.         {  
  103.                 if (null != br)  
  104.                 {  
  105.                         try   
  106.                         {  
  107.                                 br.close();  
  108.                         }   
  109.                         catch (IOException e)   
  110.                         {  
  111.                                 android.util.Log.e(TAG, "network get the latitude and longitude when closed BufferedReader ocurr IOException error", e);  
  112.                         }  
  113.                 }  
  114.         }  
  115.         return loc;  
  116.     }  
  117.   
  118. }  

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容