• 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){ }
    }
    }

  • 相关阅读:
    2019-09-09 memcache
    2019-08-26 linux
    springmvc 实体与文件同时提交时需要注意的地方
    mysql linux转win平台 遇到的坑
    使用Redis为注册中心的Dubbo微服务架构(基于SpringBoot)
    基于SpringBoot+Redis的Session共享与单点登录
    Docker运行oracle12c注意事项
    mac MyEclipse2017 CI10安装破解心得
    zookeeper,hadoop安装部署其实与防火墙无关
    VirtualBox复制的虚拟机无法获取IP解决办法
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13577844.html
Copyright © 2020-2023  润新知