0
當大量地址被轉換爲經緯度& lng時,有時結果爲空。 我認爲這個原因是使用這個URL太多,所以得到空值。 我該如何解決這個問題才能得到正確的經緯度& lng?有時候獲得經緯度和緯度爲空的谷歌地圖api json
String transformerUrl = "http://maps.google.com/maps/api/geocode/json?address=%s&language=zh-TW&sensor=false®ion=tw";
private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public String[] getLocationInfoByAddress(String address)
throws IOException, JSONException {
String[] latLngStr = new String[2];
String encodeAddress = URLEncoder.encode(address, "Utf-8");
String formatAddress = String.format(transformerUrl, encodeAddress);
InputStream is = new URL(formatAddress).openConnection().getInputStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is,
Charset.forName("UTF-8")));
String jsonText = readAll(rd);
//System.out.println(jsonText);
try {
JSONArray arr = new JSONObject(jsonText)
.getJSONArray("results");
for (int i = 0; i < arr.length(); ++i) {
if (arr.getJSONObject(i).get("geometry") != null) {
Object lat = arr.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").get("lat");
Object lng = arr.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").get("lng") ;
latLngStr[0] = lat.toString();
latLngStr[1] = lng.toString();
break;
}
}
} catch (JSONException e) {
}
} finally {
is.close();
}
return latLngStr;
}
我得到了這個「OVER_QUERY_LIMIT」。所以任何解決方案來解決它?先謝謝你。 – WhiteBanana
看到我更新的答案 – geocodezip
謝謝!我會搜索一些關於油門請求的內容。我認爲這是一個很酷的方式。 – WhiteBanana