• android 定位代码


    public class BaseStationLocateActivity extends Activity{
    TextView addressText;
    Button doLocate;
    TelephonyManager mTelephonyManager;

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addressText = (TextView) findViewById(R.id.addressText);
    doLocate = (Button) findViewById(R.id.doLocate);

    mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    doLocate.setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View v) {
    GsmCellLocation gsmCellLocation = (GsmCellLocation) mTelephonyManager.getCellLocation();
    int cid = gsmCellLocation.getCid();
    int lac = gsmCellLocation.getLac();

    int mcc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(0,3));
    int mnc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(3,5));
    try{
    //准备用于发起JSON查询的内容
    JSONObject holder = new JSONObject();
    holder.put("version", "1.1.0");
    holder.put("host", "maps.google.com");
    holder.put("request_address", true);
    JSONArray array = new JSONArray();
    JSONObject data = new JSONObject();
    data.put("cell_id", cid);
    data.put("location_area_code", lac);
    data.put("mobile_country_code", mcc);
    data.put("mobile_network_code", mnc);
    array.put(data);
    holder.put("cell_towers", array);

    //向web服务发送定位请求
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.baidu.com/loc/json");
    StringEntity se = new StringEntity(holder.toString());
    post.setEntity(se);
    HttpResponse httpResponse = client.execute(post);

    //接收并解析服务器响应
    HttpEntity entity = httpResponse.getEntity();
    BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
    StringBuffer sb = new StringBuffer();
    String result = br.readLine();
    while(result != null){
    sb.append(result);
    result = br.readLine();
    }
    JSONObject rawData = new JSONObject(sb.toString());
    JSONObject locationData = new JSONObject(rawData.getString("location"));
    getAddress(locationData.getString("latitude"), locationData.getString("longitude"));
    }catch(Exception e){}
    }
    });
    }

    //借助Google地图Web服务,根据经纬度数据得到地址名称
    private void getAddress(String lat, String lag) {
    try{
    URL url = new URL("http://maps.baidu.cn/maps/geo?key=abcdefg&q=" + lat + "," + lag);
    InputStream inputStream = url.openConnection().getInputStream();
    InputStreamReader inputReader = new InputStreamReader(inputStream, "utf-8");
    BufferedReader bufReader = new BufferedReader(inputReader);
    String line = "", lines = "";
    while((line = bufReader.readLine()) != null){
    lines += line;
    }
    if(!lines.equals("")){
    JSONObject jsonobject = new JSONObject(lines);
    JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
    for (int i = 0; i < jsonArray.length(); i++) {
    addressText.setText(addressText.getText() + " "
    + jsonArray.getJSONObject(i).getString("address"));
    }
    }
    }catch(Exception e){ }
    }
    }

  • 相关阅读:
    图片采集
    资源(先备着)
    Memories of Vladimir Arnold 摘录
    Tribute to Vladimir Arnold 摘录
    Textbooks, Testing, Training: How We Discourage Thinking 笔记
    每天都有杰出数学家去世?(挖坑)
    I'm angry——人教B版选修2-3上的一个错误
    How to Study as a Mathematics Major 的笔记
    读《桑榆忆往》做的一点笔记
    《近代欧氏几何学》笔记
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13577844.html
Copyright © 2020-2023  润新知