Android-基站定位

//获得经纬度,并查询地点
public String locationService(final TelephonyManager tm ) {
  new Thread() {
   @Override
   public void run() {
    super.run();
    System.out.println("定位开始");
    GsmCellLocation gcl = (GsmCellLocation) tm
      .getCellLocation();
    int cid = gcl.getCid();
    int lac = gcl.getLac();

    int mcc = Integer.valueOf(tm.getNetworkOperator()
      .substring(0, 3));
    int mnc = Integer.valueOf(tm.getNetworkOperator()
      .substring(3, 5));
    try {
     // 组装JSON查询字符串
     System.out.println("组装数据");
     JSONObject holder = new JSONObject();
     holder.put("version", "1.1.0");
     holder.put("host", "maps.google.com");
     // holder.put("address_language", "zh_CN");
     holder.put("request_address", true);
     JSONArray array = new JSONArray();
     JSONObject data = new JSONObject();
     data.put("cell_id", cid); // 25070
     data.put("location_area_code", lac);// 4474
     data.put("mobile_country_code", mcc);// 460
     data.put("mobile_network_code", mnc);// 0
     array.put(data);
     holder.put("cell_towers", array);
     // 创建连接,发送请求并接受回应
     DefaultHttpClient client = new DefaultHttpClient();

     HttpPost post = new HttpPost(
       "http://www.google.com/loc/json");
     StringEntity se = new StringEntity(holder
       .toString());
     post.setEntity(se);
     HttpResponse resp = client.execute(post);
     HttpEntity entity = resp.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 jsonObject = new JSONObject(sb
       .toString());
     //纬度
     JSONObject jsonObject1 = new JSONObject(jsonObject
       .getString("location"));
     System.out.println("发送数据");
     String location_message = getAddress(jsonObject1.getString("latitude"),
       jsonObject1.getString("longitude"),parentTel);
     return location_message;
    } catch (Exception e) {
     return null;
     System.out.println("网络异常");
    }
   }
  }.start();
 }
 //根据经纬度获取地理位置
 private String getAddress(String lat, String lag ) {
  try {
   URL url = new URL(
     "http://maps.google.cn/maps/geo?key=abcdefg&q="
       + lat + "," + lag);
   InputStream inputStream = url.openConnection()
     .getInputStream();
   InputStreamReader inputReader = new InputStreamReader(
     inputStream, "utf-8");
   System.out.println("获取数据");
   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());
    String message = "地理位置信息是:"
      + jsonArray.getJSONObject(0).getString(
        "address");
    return message
   }
  } catch (Exception e) {
   return null;
   System.out.println("网络出现异常");
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值