【问题标题】:Using HTTP Get request to overcome the Geocoder limits in Android使用 HTTP Get 请求克服 Android 中的 Geocoder 限制
【发布时间】:2015-04-08 00:51:13
【问题描述】:

是否可以使用 HTTP API 并执行对 Google 地图的 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);
        }

期望响应包含谷歌地图站点中的经纬度,这样我的客户端就会显示为谷歌地图的常规网络客户端。

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    Places API 请求也有配额限制,您可以在此页面查看详细信息:https://developers.google.com/places/webservice/usage

    此外,您需要一个 API 密钥来执行 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 的更多详细信息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2015-05-17
    • 2015-11-08
    • 2018-10-05
    • 2016-06-11
    • 2015-03-25
    相关资源
    最近更新 更多