2013-05-27 37 views
2

我的Google地圖活動搜索地址與Google Maps Geocoding API V3
我發現有時,即使我按順序重複搜索多次,Google地圖響應在與數據連接連接時仍爲OVER_QUERY_LIMIT。
它也發生在應用程序安裝在設備上後的第一次搜索中。
當我連接到無線網絡,它完美的作品。
這是我的代碼。
搜索方法:谷歌地圖地理編碼API V3搜索與WiFi無誤,但響應OVER_QUERY_LIMIT與數據連接

public static JSONObject getAddressInfo(String sAddress) { 
    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" + sAddress + "&region=it&language=it&sensor=false"); 
    HttpClient client = new DefaultHttpClient(); 
    HttpResponse response; 
    StringBuilder stringBuilder = new StringBuilder(); 

    try { 
     response = client.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 
     InputStream stream = entity.getContent(); 
     int b; 
     while ((b = stream.read()) != -1) { 
      stringBuilder.append((char) b); 
     } 
    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 

    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject = new JSONObject(stringBuilder.toString()); 
     Log.d("Google Geocoding Response", stringBuilder.toString()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return jsonObject; 
} 

迴應管理層:

JSONObject jsonObject = Utils.getAddressInfo(Utils.strToUrl(inputName.getText().toString().trim())); 
    try { 
     String sStatus = jsonObject.getString("status"); 
     if (sStatus.equals("OK")) { 
      lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng"); 
      lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat");      
      bdlData.putDouble("lat", lat); 
      bdlData.putDouble("lng", lng); 
      bdlData.putFloat("dZoom", dZoom); 
      message.setData(bdlData); 
      mapHandler.sendMessage(message); 
     } else if (sStatus.equals("ZERO_RESULTS")) { 
      runMsgOnUIThread("Nessun risultato trovato."); 
     } else if (sStatus.equals("OVER_QUERY_LIMIT")) { 
      runMsgOnUIThread("Impossibile effettuare la ricerca al momento. Riprovare fra qualche secondo."); 
     } else if (sStatus.equals("REQUEST_DENIED")) { 
      runMsgOnUIThread("Richiesta non accettata. Riprovare."); 
     } else if (sStatus.equals("INVALID_REQUEST")) { 
      runMsgOnUIThread("Indirizzo non esistente."); 
     } else if (sStatus.equals("UNKNOWN_ERROR")) { 
      runMsgOnUIThread("Impossibile effettuare la ricerca al momento. Riprovare.");     
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

回答

6

您的問題最有可能駐留在您的移動運營商。絕大多數運營商使用稱爲NAT overloading的技術,並將相同的外部IP分配給多個設備。如果您的運營商將大量設備分配給單個IP,並且其中一些設備使用類似的服務,則每個人都會遇到問題,因爲所有請求似乎都源於同一個IP。

10 * 200ms查詢的成功似乎與服務器端OVER_QUERY_LIMIT標誌的到期有關,因爲它隱含在此文檔中(Usage Limits for Google Maps API Web Services),這表明在收到此狀態後應該2秒後重新查詢,看看你是否超出了你的日常使用量,或者你發送了太多請求。

這不會通過wifi發生,因爲您的手機擁有自己的或多或少的獨特IP。

+0

感謝您的支持!我認爲這可能是... ...奇怪的是,有了相同的連接:a)Google地圖應用程序完美工作; b)Android Geocoder類完美地工作(我不實際使用它,因爲其他問題)... 現在我該怎麼辦? – UmbySlipKnot

+1

谷歌地圖應用程序肯定使用Google的客戶端ID來處理他們的請求,所以他們可能對應用程序沒有限制。如果Geocoder類也能正常工作,那麼它或者使用該客戶端ID,或者更有效地限制和緩存請求。 – aBrav

+0

我明白。那麼你有什麼建議我處理這個問題?有人告訴我不要使用Geocoder類:http://stackoverflow.com/a/16656050/1769013 – UmbySlipKnot

0

我發現了一個變通方法,常常(並不總是)解決了這個問題:如果我得到OVER_QUERY_LIMIT重新查詢谷歌每200毫秒,最多的10倍。