java - 使用 HTTP Get 请求来克服 Android 中的 Geocoder 限制

标签 java android google-maps

是否可以使用 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 请求,例如,在 AsyncTaskdoInBackground() 方法中。

您还可以访问this tutorial了解有关如何在 Android 中使用 Places API 的更多详细信息。

关于java - 使用 HTTP Get 请求来克服 Android 中的 Geocoder 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495228/

相关文章:

java - 如何处理 AWT 框架中的关闭按钮

java - Eclipse 无法识别 Android 设备

java - ConnectyCube 检索对话框列表返回空列表

javascript - 在重新加载时删除标记时,react-native-maps 中的 callout View 不会消失

java - 需要有关使用 Android Pattern 检测车牌号的帮助

javascript - 使用 JSP、Java 和 Javascript 的带有数据库数据的 HTML 表

android - 更改 Android 模拟器大小以适合我的屏幕

ios - 在没有标签的情况下使用 MapKit 或 Google Maps iOS SDK

Android PlacePicker 不返回选择的地址

java - 使用eclipse逐行读取文本文件java