开发者社区> 问答> 正文

如何根据基站获取移动端地理位置

阿里云根据基站获取,移动端位置

展开
收起
哎呀呀呀呀 2016-10-17 17:33:38 3920 0
1 条回答
写回答
取消 提交回答
  • 阿里云ping https://ping.gaomeluo.com/aliyun/

    /**

    • 获取基站信息
    • @return
    • @throws Exception
      */

    public SCell getCellInfo() throws Exception{
    SCell cell = new SCell();
    TelephonyManager mTelNet = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
    if (location == null)
    throw new Exception("基站信息为空");
    String operator = mTelNet.getNetworkOperator();
    int mcc = Integer.parseInt(operator.substring(0, 3));
    int mnc = Integer.parseInt(operator.substring(3));
    int cid = location.getCid();
    int lac = location.getLac();
    cell.MCC = mcc;
    cell.MNC = mnc;
    cell.LAC = lac;
    cell.CID = cid;
    return cell;
    }

    /**

    • 通过基站信息获取经纬度
    • @param cell
    • @return
    • @throws Exception
      */

    private SItude getItude(SCell cell) throws Exception {
    SItude itude = new SItude();

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.google.com/loc/json");
    try {
    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);
    holder.put("radio_type", "gsm");
    holder.put("carrier", "HTC");

    JSONObject tower = new JSONObject();
    tower.put("mobile_country_code", cell.MCC);
    tower.put("mobile_network_code", cell.MNC);
    tower.put("cell_id", cell.CID);
    tower.put("location_area_code", cell.LAC);

    JSONArray towerarray = new JSONArray();
    towerarray.put(tower);
    holder.put("cell_towers", towerarray);

    StringEntity query = new StringEntity(holder.toString());
    post.setEntity(query);

    HttpResponse response = client.execute(post);
    HttpEntity entity = response.getEntity();
    BufferedReader buffReader = new BufferedReader(
    new InputStreamReader(entity.getContent()));
    StringBuffer strBuff = new StringBuffer();
    String result = null;
    while ((result = buffReader.readLine()) != null) {
    strBuff.append(result);
    }
    JSONObject json = new JSONObject(strBuff.toString());
    JSONObject subjosn = new JSONObject(json.getString("location"));

    itude.latitude = subjosn.getString("latitude");
    itude.longitude = subjosn.getString("longitude");

    Log.i("Itude", itude.latitude + itude.longitude);

    } catch (Exception e) {
    Log.e(e.getMessage(), e.toString());
    throw new Exception("" + e.getMessage());
    } finally {
    post.abort();
    client = null;
    }

    return itude;
    }

    /**

    • 通过经纬度到Google map上获取地理位置
    • @param itude
    • @return
    • @throws Exception
      */

    private String getLocation(SItude itude) throws Exception {
    String resultString = "";
    String urlString = String.format(
    "http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s",
    itude.latitude, itude.longitude);
    Log.i("URL", urlString);
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(urlString);
    try {
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    BufferedReader buffReader = new BufferedReader(
    new InputStreamReader(entity.getContent()));
    StringBuffer strBuff = new StringBuffer();
    String result = null;
    while ((result = buffReader.readLine()) != null) {
    strBuff.append(result);
    }
    resultString = strBuff.toString();
    if (resultString != null && resultString.length() > 0) {
    JSONObject jsonobject = new JSONObject(resultString);
    JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
    .toString());
    resultString = "";
    for (int i = 0; i < jsonArray.length(); i++) {
    resultString = jsonArray.getJSONObject(i).getString(
    "address");
    }
    }
    } catch (Exception e) {
    throw new Exception("sd:" + e.getMessage());
    } finally {
    get.abort();
    client = null;
    }
    return resultString;
    }

    public class SCell {
    public int MCC;
    public int MNC;
    public int LAC;
    public int CID;
    }

    public class SItude {
    public String latitude;
    public String longitude;
    }

    2019-07-17 20:17:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手机QQ移动网络接入优化之路 立即下载
高可靠宽带RFID定位 立即下载
从500万到2.4亿,手机QQ移动网络接入优化之路 立即下载