zoukankan      html  css  js  c++  java
  • android 基站定位

      1. package cn.LocationStation;  
      2.   
      3. import java.io.BufferedReader;  
      4. import java.io.InputStream;  
      5. import java.io.InputStreamReader;  
      6. import java.net.URL;  
      7. import java.util.Date;  
      8.   
      9. import org.apache.http.HttpEntity;  
      10. import org.apache.http.HttpResponse;  
      11. import org.apache.http.client.methods.HttpPost;  
      12. import org.apache.http.entity.StringEntity;  
      13. import org.apache.http.impl.client.DefaultHttpClient;  
      14. import org.json.JSONArray;  
      15. import org.json.JSONObject;  
      16.   
      17.   
      18. import android.app.Activity;  
      19. import android.content.Context;  
      20. import android.os.Bundle;  
      21. import android.telephony.TelephonyManager;  
      22. import android.telephony.gsm.GsmCellLocation;  
      23. import android.view.View;  
      24. import android.widget.Button;  
      25. import android.widget.TextView;  
      26.   
      27. public class LocationStation extends Activity {  
      28.     TextView mTextView;  
      29.     Button mButton;  
      30.     TelephonyManager tm;  
      31.   
      32.     /** Called when the activity is first created. */  
      33.     @Override  
      34.     public void onCreate(Bundle savedInstanceState) {  
      35.         super.onCreate(savedInstanceState);  
      36.         setContentView(R.layout.main);  
      37.   
      38.         mTextView = (TextView) findViewById(R.id.textView001);  
      39.         mButton = (Button) findViewById(R.id.Button001);  
      40.         tm = (TelephonyManager) this  
      41.                 .getSystemService(Context.TELEPHONY_SERVICE);  
      42.   
      43.         mButton.setOnClickListener(new Button.OnClickListener() {  
      44.   
      45.             @Override  
      46.             public void onClick(View v) {  
      47.                 // TODO Auto-generated method stub  
      48.                 GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();  
      49.                 int cid = gcl.getCid();  
      50.                 int lac = gcl.getLac();  
      51.   
      52.                 int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,  
      53.                         3));  
      54.                 int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,  
      55.                         5));  
      56.                 try {  
      57.                     // 组装JSON查询字符串  
      58.                     JSONObject holder = new JSONObject();  
      59.                     holder.put("version""1.1.0");  
      60.                     holder.put("host""maps.google.com");  
      61.                     // holder.put("address_language", "zh_CN");  
      62.                     holder.put("request_address"true);  
      63.   
      64.                     JSONArray array = new JSONArray();  
      65.                     JSONObject data = new JSONObject();  
      66.                     data.put("cell_id", cid); // 25070  
      67.                     data.put("location_area_code", lac);// 4474  
      68.                     data.put("mobile_country_code", mcc);// 460  
      69.                     data.put("mobile_network_code", mnc);// 0  
      70.                     array.put(data);  
      71.                     holder.put("cell_towers", array);  
      72.   
      73.                     // 创建连接,发送请求并接受回应  
      74.                     DefaultHttpClient client = new DefaultHttpClient();  
      75.   
      76.                     HttpPost post = new HttpPost(  
      77.                             "http://www.google.com/loc/json");  
      78.   
      79.                     StringEntity se = new StringEntity(holder.toString());  
      80.   
      81.                     post.setEntity(se);  
      82.                     HttpResponse resp = client.execute(post);  
      83.   
      84.                     HttpEntity entity = resp.getEntity();  
      85.   
      86.                     BufferedReader br = new BufferedReader(  
      87.                             new InputStreamReader(entity.getContent()));  
      88.                     StringBuffer sb = new StringBuffer();  
      89.                     String result = br.readLine();  
      90.   
      91.                     while (result != null) {  
      92.   
      93.                         sb.append(result);  
      94.                         result = br.readLine();  
      95.                     }  
      96.                     JSONObject jsonObject = new JSONObject(sb.toString());  
      97.   
      98.                     JSONObject jsonObject1 = new JSONObject(jsonObject  
      99.                             .getString("location"));  
      100.   
      101.                     getAddress(jsonObject1.getString("latitude"), jsonObject1  
      102.                             .getString("longitude"));  
      103.   
      104.                     //mTextView.setText(sb.toString());  
      105.                 } catch (Exception e) {  
      106.                     // TODO: handle exception  
      107.                 }  
      108.   
      109.             }  
      110.   
      111.         });  
      112.     }  
      113.   
      114.     void getAddress(String lat, String lag) {  
      115.         try {  
      116.   
      117.             URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="  
      118.                     + lat + "," + lag);  
      119.             InputStream inputStream = url.openConnection().getInputStream();  
      120.             InputStreamReader inputReader = new InputStreamReader(inputStream,  
      121.                     "utf-8");  
      122.             BufferedReader bufReader = new BufferedReader(inputReader);  
      123.   
      124.             String line = "", lines = "";  
      125.   
      126.             while ((line = bufReader.readLine()) != null) {  
      127.                 lines += line;  
      128.             }  
      129.             if (!lines.equals("")) {  
      130.   
      131.                 JSONObject jsonobject = new JSONObject(lines);  
      132.                 JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")  
      133.                         .toString());  
      134.                 for (int i = 0; i < jsonArray.length(); i++) {  
      135.   
      136.                     mTextView.setText(mTextView.getText() + " "  
      137.                             + jsonArray.getJSONObject(i).getString("address"));  
      138.                 }  
      139.   
      140.             }  
      141.   
      142.         } catch (Exception e) {  
      143.             ;  
      144.         }  
      145.   
      146.     }  
      147. }
  • 相关阅读:
    【PHP】php基础回顾
    【PHP】MVC架构
    【OpenGL学习】使用VBO和FBO
    【OpenGL学习】使用Shader做图像处理
    hdu 杭电 1242 Rescue 果枫
    并查集模板 果枫
    数组结构体中排序 果枫
    hdu 杭电 1728 逃离迷宫 果枫
    hdu 杭电 1241 Oil Deposits 果枫
    hdu 杭电 2216 Game III 果枫
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3606450.html
Copyright © 2011-2022 走看看