2012-08-15 66 views
2

這有點寬泛,但我們現在去了:我的三角測量算法出現了一個非常奇怪的問題。有時候,它會返回正確的經度/緯度,然後經過一段時間後,它會返回錯誤的緯度/經度。更奇怪的是,我無法預測什麼時候會發生這個錯誤,也不能重現它。即使我不更改我的代碼中的行(即接收正確的值,然後錯誤,然後正確,等等),它也會發生。在Android中使用三角測量收到的錯誤位置

我正在使用谷歌GLM服務,發送設備LAC(位置區號)和塔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的服務類,調用到1分鐘的延遲使用的PendingIntent。調用它的線程保存SharedPreferences中的緯度/經度值,然後在我所有的視圖中使用它。

我想知道如果我已經實施了錯誤算法的方法,或者如果我錯過了一個過程中的技巧。目前,我的正確aproximated經/緯度值緯度= -23經度= -46,但我收到(有時)緯度= 17經度= 81的值。有人能給我提示發生了什麼嗎?

回答

0

好的。我自己找到了解決方案。開始了。 三角測量過程基本上是一個三參數組合過程。根據我所做的一些搜索,我推斷可以使用不同類型的數據組合。我使用的組合是與塔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; 
} 

有了這個新的代碼,請注意,我用的是谷歌位置服務的JSON,返回一個JSON對象。我應該說,這是一個非常棘手的問題,我需要更多地瞭解所有的機制,但至少,這是一個有效的代碼。希望它能幫助別人。

更新:我也想在以下上下文中完成此答案:三角洲通過移動網絡(GSM,UMTS,LTE等)使用無線電接入。我很確定,現在這些價值的影響來自於無線電波對環境有很大幹擾的事實(牆壁,建築物,相鄰的塔等)