是否可以使用 HTTP API 并对 Google map 执行 HTTP Get 请求,以克服使用 Geocoder API 在请求地点经纬度时的限制?
类似的东西-
URL url = new URL("https://www.google.com/maps/place/Paris/");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
System.out.println("Value" + connection.getResponseCode());
System.out.println(connection.getResponseMessage());
System.out.println("content"+connection.getContent());
或
URL url = new URL("https://www.google.com/maps/place/Paris/");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String strTemp = "";
while (null != (strTemp = br.readLine())) {
System.out.println(strTemp);
}
期望响应包含该地点的经纬度,就像 Google map 网站一样,这样我的客户端就会显示为 Google map 的常规 Web 客户端。
最佳答案
Places API 请求也有配额限制,您可以在此页面查看详细信息:https://developers.google.com/places/webservice/usage
此外,您需要一个 API key 来执行 Places API 请求,在 Android 中执行 Places API URL 请求的示例方法应如下所示:
URL placeUrl = new URL("https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&key=AddYourOwnKeyHere");
HttpURLConnection connection = (HttpURLConnection)placeUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = null;
InputStream inputStream = connection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
Log.d(TAG, buffer.toString());
}
else {
Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
}
您应该在后台线程中执行此 URL 请求,例如,在 AsyncTask
的 doInBackground()
方法中。
您还可以访问this tutorial了解有关如何在 Android 中使用 Places API 的更多详细信息。
关于java - 使用 HTTP Get 请求来克服 Android 中的 Geocoder 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495228/