【问题标题】:Wrong position received with triangulation in Android在 Android 中通过三角测量收到错误的位置
【发布时间】:2012-08-12 04:45:51
【问题描述】:

这有点宽泛,但我们开始吧:我的三角测量算法出现了一个非常奇怪的问题。有时,它返回正确的纬度/经度,然后,一段时间后,它返回错误的纬度/经度。更奇怪的是,我无法预测何时会发生此错误,也无法重现它。即使我不更改代码中的行(收到正确的值,然后是错误的,然后是正确的,等等......),它也会发生。

我正在使用 google GLM 服务,将设备 LAC(位置区号)和 Tower ID 发送到三角我的位置。我的算法的核心方法如下:

private double[] getPositionByTriangle(int lac, int cid) {
    int shortcid = cid & 0xffff;
    double location[] = new double[2];
    try {
        String surl = "http://www.google.com/glm/mmap";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(surl);
        httppost.setEntity(new CellIDRequestEntity(shortcid, lac));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        DataInputStream dis = new DataInputStream(entity.getContent());
        // Read some prior data
        dis.readShort();
        dis.readByte();
        // Read the error-code
        int errorCode = dis.readInt();
        if (errorCode == 0) {
            location[0] = (double) dis.readInt() / 1000000D;
            location[1] = (double) dis.readInt() / 1000000D;
        }
    } catch (Exception e) {}
    return location;
}

额外信息,可能会有帮助:此方法用于扩展 Android Service 的类中,通过 PendingIntent 延迟 1 分钟调用。调用它的线程将纬度/经度值保存在 SharedPreferences 中,然后我在所有视图中使用它。

我想知道我是否使用错误的算法实现了该方法,或者我错过了过程中的技巧。目前,我正确的近似纬度/经度值为 latitude = -23longitude = -46,但我收到(有时)latitude = 17经度 = 81。有人可以告诉我发生了什么吗?

【问题讨论】:

    标签: android geolocation triangulation


    【解决方案1】:

    好的。我自己找到了解决方案。开始了。 三角测量过程基本上是一个三参数组合过程。根据我所做的一些搜索,我推断您可以使用不同类型的数据组合。我使用的组合是与塔 ID(设备握手的塔收发器)、位置代码(区号和国家代码)和位置区号的组合。所以,我的新代码如下所示:

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation.requestLocationUpdate();
    location = (GsmCellLocation) tm.getCellLocation();
    networkOperator = tm.getNetworkOperator();
    cellID = location.getCid();
    lac = location.getLac();
    
    public double[] getLocationByMCCMNC(String mcc, String mnc, String lac, String cellId, boolean requestAddress) throws IOException {
    
        final String data = "{\"cell_towers\": [{\"location_area_code\": \""
            + lac + "\", \"mobile_network_code\": \"" + mnc
            + "\", \"cell_id\": \"" + cellId
            + "\", \"mobile_country_code\": \"" + mcc
            + "\"}], \"version\": \"1.1.0\", \"request_address\": \""
            + requestAddress + "\"}";
    
        URL anUrl = new URL("https://www.google.com/loc/json");
        HttpURLConnection conn = (HttpURLConnection) anUrl.openConnection();
    
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-type", "application/jsonrequest");
    
        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes());
        out.close();
    
        InputStream in = conn.getInputStream();
    
        StringBuilder builder = new StringBuilder();
        Reader reader = new InputStreamReader(in);
        char[] buf = new char[1024];
        int read = 0;
        while ((read = reader.read(buf)) >= 0) {
            builder.append(String.valueOf(buf, 0, read));
        }
        in.close();
        conn.disconnect();
    
        double location[] = new double[2];
        try {
            JSONObject loc = new JSONObject(builder.toString()).getJSONObject("location");
            location[0] = Double.valueOf(loc.getDouble("latitude"));
            location[1] = Double.valueOf(loc.getDouble("longitude"));
        } catch (JSONException e) {
            location[0] = -1;
        location[1] = -1;
        }
        return location;
    }
    

    使用这个新代码,请注意我使用的是 Google Location JSON 服务,它返回一个 JSON 对象。我应该说这是一个非常棘手的问题,我需要更多地搜索以了解所有机制,但至少,这是一个有效的代码。希望它可以帮助某人。

    更新:我也想在以下情况下完成此答案:三角测量通过移动网络(GSM、UMTS、LTE 等)使用无线电接入。我现在很确定这些价值波动来自无线电波对环境(墙壁、建筑物、相邻的塔楼等)有很大干扰的事实

    【讨论】:

      猜你喜欢
      • 2015-06-30
      • 2013-05-05
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多